title: jQuery - hover to trigger hover/mouseover on refresh?
body: <p>I'm using <a href="http://cherne.net/brian/resources/jquery.hoverIntent.html" rel="nofollow">HoverIndent</a> jQuery plugin for my dropdown menu, basically I hover on #link and it shows #submenu.</p>
  
  <p>Unfortunately it doesn't work as expected. Everything looks fine at first but there's this case where I leave my cursor on #link, hit F5 (page refresh) and after refresh the HoverIndent isn't triggered, I have to move my cursor out of the #link, then move it back over to make HoverIndent working. What's even worse it's usually not working straight away (I have to wait a while for page to load). I've been trying everything including disabling all other JS, but without luck. </p>
  
  <p>I've been investigating this for a while and looks like this code (basic jQuery no HoverIndent here):</p>
  
  <pre><code>$("#link").on("mouseover", function() {
    console.log("hover");
  })
  </code></pre>
  
  <p>Or this:</p>
  
  <pre><code>$("#link").hover(function(){
    console.log("hover");
  })
  </code></pre>
  
  <p>Works the same, so when I leave cursor or #link and hit F5 it doesn't log "hover" to console after refresh.</p>
  
  <p>Be aware it works on jsfiddle etc. because there are pages loading in iframes, but it doesn't work locally.</p>
  
  <p>Also it seems to be working on HoverIdent's page, I have absolutely no idea why. Am I right hover/mouseover shouldn't fire right after page load? If yes how to force them to and what's wrong with my HoverIndent? </p>
  
  <p><strong>Big update</strong></p>
  
  <p>It gets really weird. I've downloaded HTML5 Boilerplate and copied the whole HoverIndent demo page. The same hoverIntent.js file served locally doesn't seem to work while works from vendors server:</p>
  
  <pre><code>&lt;!-- uncomment this line and everything works? the hell? :D --&gt;
  &lt;!-- &lt;script type="text/javascript" src="http://cherne.net/brian/resources/jquery.hoverIntent.js"&gt;&lt;/script&gt; --&gt;
  
  &lt;!-- same code but from localhost and doesn't work --&gt;
  &lt;script type="text/javascript" src="js/jquery.hoverIntent.js"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>Here's the whole code (note you need to save <a href="http://cherne.net/brian/resources/jquery.hoverIntent.js" rel="nofollow">http://cherne.net/brian/resources/jquery.hoverIntent.js</a> in js directory next to index.html:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html class="no-js"&gt;
  &lt;head&gt;
      &lt;script src="js/vendor/jquery-1.10.2.min.js"&gt;&lt;/script&gt;
  
      &lt;!-- uncomment this line and everything works? the hell? :D --&gt;
      &lt;!-- &lt;script type="text/javascript" src="http://cherne.net/brian/resources/jquery.hoverIntent.js"&gt;&lt;/script&gt; --&gt;
  
      &lt;!-- same code but from localhost and doesn't work --&gt;
      &lt;script type="text/javascript" src="js/jquery.hoverIntent.js"&gt;&lt;/script&gt;
  
      &lt;style type="text/css" media="screen"&gt;
          body{background-color:#fff;font-family:Verdana,Arial,sans-serif;font-size:1em;color:#000;margin:0;padding:0}a{outline:0}h1{margin:0;padding:.4em 0 .45em .6em;font-size:.9em;background-color:#996;color:#cc9;white-space:nowrap}h1 a{color:#cc9;text-decoration:none}h1 a:hover{color:#fff}h2{width:100%;border-bottom:#cc9 1px solid;margin:0 0 .5em;padding:1em 0 0;font-size:1.2em}h3{display:block;margin:0;padding:0 0 1px;font-size:1em;font-weight:700}h4{display:block;margin:0;padding:0 0 1px;text-transform:uppercase;font-size:.85em;font-weight:700;color:#996}img{border:none}p{margin:0;padding:0 0 1em;clear:both}ul{margin:0;padding:0 0 0 1em;clear:both;list-style-type:none}ul li{margin:0;padding:0 0 0 1em;background-image:url(images/bullet.gif);background-repeat:no-repeat;background-position:0 .15em}dl{margin:0;padding:0 0 2em;clear:both}dt{margin:0;padding:1em 0 0 1em;font-weight:700}dd{margin:0;padding:0 0 0 1em}#nav1{display:block;list-style-type:none;margin:0;padding:0 0 .25em;background-color:#cc9;white-space:nowrap}#nav1 li{display:inline;margin:0;padding:0 .75em 0 .25em;background-image:none}#nav1 li a{font-size:.9em;font-weight:700;padding:0 .3em;text-decoration:none;color:#787850}#nav1 li a:hover{background-color:#996;color:#000}#nav1 li a.current{color:#fff}#pageContent{font-size:.9em;line-height:1.3em;max-width:55em}#main{padding:1em 1em 3em;color:#000}#main a{color:#787850;text-decoration:underline}#main a:hover{text-decoration:none}#main h2.section{border-top:#cc9 1px solid;padding-top:.5em}#main p.section{border-top:#cc9 1px solid;margin-top:3em}#PICTURES #main a{float:left;display:block;width:33%;text-decoration:none;text-align:center;padding:.5em 0;margin:0 0 1em}#PICTURES #main a:hover{background:#e6e6cc}#PICTURES #main a img{display:block;width:93%;margin:0 auto;padding:0}#PICTURES #main span{display:block;width:92% margin:0 auto}div.job{clear:both;margin:1em 0 0;padding:0 0 1em}div.job h3{float:left;width:60%}div.job .what,div.job .when,div.job .where{float:left;display:block}div.job .what{padding:0 0 1px;width:40%;text-align:right;color:#996}div.job .when,div.job .where{position:relative;width:60%;text-transform:uppercase;font-size:.85em;line-height:1.5em;height:1.6em;background-color:#e6e6cc}div.job .when{width:40%;text-align:right}div.job p{padding:.5em 0}#RESUME h4{clear:both;padding:.5em 0 0 2.3em;font-weight:400;font-size:.85em}#RESUME #main ul{padding-bottom:.5em}#RESUME #main div.job ul{padding-bottom:.2em}#RESUME div.job ul li{background-image:none;padding-top:.3em;padding-bottom:.3em}#RESUME div.job p{clear:both;padding:.2em 0 .5em}#RESUME div.modified{clear:both;margin:0;padding:1em 0 0;color:#999;text-align:center}#PORTFOLIO h2{padding-top:.2em;font-size:1.2em;border-bottom:none}div.job p img{clear:left;float:left;width:100px;margin:0 .5em 0 0}#PORTFOLIO ul.screenshots{margin:0;padding:0;display:block;clear:both;list-style-type:none}#PORTFOLIO ul.screenshots li{display:block;margin:.5em 0;padding:1em 0;width:44em;float:left;background:#e6e6cc}#PORTFOLIO ul.screenshots li img{float:left;margin:0 1em}#PORTFOLIO ul.screenshots li span{display:block;padding-right:.5em}#PORTFOLIO dl{display:block;width:21em;float:left;clear:none}#PORTFOLIO dt a{font-weight:700}#RESOURCES #main li{padding-bottom:1em}#RESOURCES .download{text-align:center}#RESOURCES pre{background:#eee;color:#666;padding:1.5em;margin:0;font-size:1em;font-family:verdana;overflow:auto}#RESOURCES pre span{color:#699;font-family:arial}#RESOURCES #main .demo li{padding-bottom:0}ul.demo{display:block;width:100%;height:75px;padding:0;margin:0;background:#9cc;list-style-type:none}ul.demo li{background:#fcc;display:block;width:25%;height:50px;padding:0;margin:0;float:left;position:relative;overflow:hidden;cursor:default;font-size:.9em;line-height:1.1em}ul.demo li.p2{background:#ffc}ul.demo li.p3{background:#cfc}ul.demo li.p4{background:#ccf}ul.demo li span{display:block;margin:4px;background:#eef;cursor:default}
      &lt;/style&gt;
  
      &lt;script type="text/javascript" charset="utf-8"&gt;
      $(document).ready(function(){
          $("#demo1 li").hover(makeTall,makeShort);
          $("#demo2 li").hoverIntent(makeTall,makeShort);
          $("#demo3 li").hoverIntent(toggleHeight);
          $("#demo4").hoverIntent(makeTall,makeShort,'li');
          $("#demo5").hoverIntent(toggleHeight,'li');
          $("#demo6").hoverIntent({
              over: makeTall,
              out: makeShort,
              selector: 'li'
          });
      });
  
      function makeTall(){$(this).animate({"height":75},200);}
      function makeShort(){$(this).animate({"height":50},200);}
      function toggleHeight(){var h=(parseInt($(this).css('height'),10) &gt; 50) ? 50 : 75; $(this).animate({"height":h},200);}
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div id="pageContent"&gt;
  
          &lt;div id="main"&gt;
  
              &lt;h2&gt;What is hoverIntent?&lt;/h2&gt;
              &lt;p&gt;hoverIntent is a plug-in that attempts to determine the user's intent... like a crystal ball, only with mouse movement! It is similar to &lt;a href="http://api.jquery.com/hover/"&gt;jQuery's hover method&lt;/a&gt;. However, instead of calling the handlerIn function immediately, hoverIntent waits until the user's mouse slows down enough before making the call.&lt;/p&gt;
              &lt;p&gt;Why? To delay or prevent the accidental firing of animations or ajax calls. Simple timeouts work for small areas, but if your target area is large it may execute regardless of intent. That's where hoverIntent comes in...&lt;/p&gt;
              &lt;p class="download"&gt;&lt;a href="jquery.hoverIntent.js"&gt;Download hoverIntent v1.8.0 (fully-commented, uncompressed)&lt;/a&gt;&lt;/p&gt;
              &lt;p class="download"&gt;&lt;a href="jquery.hoverIntent.minified.js"&gt;Download hoverIntent v.1.8.0 (minified)&lt;/a&gt;&lt;/p&gt;
              &lt;p class="download"&gt;&lt;a href="https://github.com/briancherne/jquery-hoverIntent"&gt;briancherne/jquery-hoverIntent (GitHub)&lt;/a&gt;&lt;/p&gt;
  
              &lt;h2&gt;Examples&lt;/h2&gt;
  
              &lt;noscript&gt;&lt;p&gt;&lt;em&gt;If you can see this message &lt;strong&gt;JavaScript is disabled&lt;/strong&gt;. This plug-in requires JavaScript to be enabled in order for the examples to work. (This is really a note to myself so the next time I look at my web site with JavaScript accidentally turned off I don't freak out and wonder why it's not working)&lt;/em&gt;&lt;/p&gt;&lt;/noscript&gt;
  
              &lt;h3&gt;jQuery's hover (for reference)&lt;/h3&gt;
              &lt;pre&gt;$("#demo1 li").hover( makeTall, makeShort );&lt;/pre&gt;
              &lt;ul class="demo" id="demo1"&gt;
                  &lt;li class="p1"&gt;&amp;nbsp;&lt;/li&gt;
                  &lt;li class="p2"&gt;&amp;nbsp;&lt;/li&gt;
                  &lt;li class="p3"&gt;&amp;nbsp;&lt;/li&gt;
                  &lt;li class="p4"&gt;&lt;span&gt;hover ignores over/out events from children&lt;/span&gt;&lt;/li&gt;
              &lt;/ul&gt;
              &lt;p&gt;jQuery's built-in hover calls handlerIn and handlerOut functions immediately. If you move your cursor back and forth quickly across the tiles you'll see how the immediate execution can lead to problems.&lt;/p&gt;
  
              &lt;h3&gt;.hoverIntent( handlerIn, handlerOut )&lt;/h3&gt;
              &lt;pre&gt;$("#demo2 li").hoverIntent( makeTall, makeShort );&lt;/pre&gt;
              &lt;ul class="demo" id="demo2"&gt;
                  &lt;li class="p1"&gt;&amp;nbsp;&lt;/li&gt;
                  &lt;li class="p2"&gt;&amp;nbsp;&lt;/li&gt;
                  &lt;li class="p3"&gt;&amp;nbsp;&lt;/li&gt;
                  &lt;li class="p4"&gt;&lt;span&gt;hoverIntent also ignores over/out events from children&lt;/span&gt;&lt;/li&gt;
              &lt;/ul&gt;
              &lt;p&gt;hoverIntent is interchangeable with jQuery's hover. It can use the same exact handlerIn and handlerOut functions. It passes the same &lt;strong&gt;this&lt;/strong&gt; and &lt;strong&gt;event&lt;/strong&gt; objects to those functions.&lt;/p&gt;
  
              &lt;h3&gt;.hoverIntent( handlerInOut )&lt;/h3&gt;
              &lt;pre&gt;$("#demo3 li").hoverIntent( toggleHeight );&lt;/pre&gt;
              &lt;ul class="demo" id="demo3"&gt;
                  &lt;li class="p1"&gt;&amp;nbsp;&lt;/li&gt;
                  &lt;li class="p2"&gt;&amp;nbsp;&lt;/li&gt;
                  &lt;li class="p3"&gt;&amp;nbsp;&lt;/li&gt;
                  &lt;li class="p4"&gt;&amp;nbsp;&lt;/li&gt;
              &lt;/ul&gt;
              &lt;p&gt;hoverIntent can also take a single handlerInOut, just like jQuery's hover.&lt;/p&gt;
  
              &lt;h3&gt;.hoverIntent( handlerIn, handlerOut, selector )&lt;/h3&gt;
              &lt;pre&gt;$("#demo4").hoverIntent( makeTall, makeShort, 'li' );&lt;/pre&gt;
              &lt;ul class="demo" id="demo4"&gt;
                  &lt;li class="p1"&gt;&amp;nbsp;&lt;/li&gt;
                  &lt;li class="p2"&gt;&amp;nbsp;&lt;/li&gt;
                  &lt;li class="p3"&gt;&amp;nbsp;&lt;/li&gt;
                  &lt;li class="p4"&gt;&amp;nbsp;&lt;/li&gt;
              &lt;/ul&gt;
              &lt;p&gt;Unlike jQuery's hover, hoverIntent supports event delegation! Just pass in a selector of a descendant element.&lt;/p&gt;
  
              &lt;h3&gt;.hoverIntent( handlerInOut, selector )&lt;/h3&gt;
              &lt;pre&gt;$("#demo5").hoverIntent( toggleHeight, 'li' );&lt;/pre&gt;
              &lt;ul class="demo" id="demo5"&gt;
                  &lt;li class="p1"&gt;&amp;nbsp;&lt;/li&gt;
                  &lt;li class="p2"&gt;&amp;nbsp;&lt;/li&gt;
                  &lt;li class="p3"&gt;&amp;nbsp;&lt;/li&gt;
                  &lt;li class="p4"&gt;&amp;nbsp;&lt;/li&gt;
              &lt;/ul&gt;
              &lt;p&gt;Unlike jQuery's hover, hoverIntent supports event delegation with handlerInOut.&lt;/p&gt;
  
              &lt;h3&gt;.hoverIntent( object )&lt;/h3&gt;
      &lt;pre&gt;
      $("#demo6").hoverIntent({
          over: makeTall,
          out: makeShort,
          selector: 'li'
      });
      &lt;/pre&gt;
              &lt;ul class="demo" id="demo6"&gt;
                  &lt;li class="p1"&gt;&amp;nbsp;&lt;/li&gt;
                  &lt;li class="p2"&gt;&amp;nbsp;&lt;/li&gt;
                  &lt;li class="p3"&gt;&amp;nbsp;&lt;/li&gt;
                  &lt;li class="p4"&gt;&amp;nbsp;&lt;/li&gt;
              &lt;/ul&gt;
              &lt;p&gt;To control hoverIntent more precisely and override the default configuration options, pass it an object as the first parameter. The object must at least contain an "over" function. If the "over" function is sent alone, it will act just like handlerInOut.&lt;/p&gt;
  
  
              &lt;h2&gt;Common Configuration Options&lt;/h2&gt;
              &lt;p&gt;These are the common options you'll want to use. Note, nothing prevents you from sending &lt;a href="http://api.jquery.com/jQuery.noop/"&gt;an empty function&lt;/a&gt; as the handlerIn or handlerOut functions.&lt;/p&gt;
  
              &lt;h3&gt;over:&lt;/h3&gt;
              &lt;p&gt;Required. The handlerIn function you'd like to call on "mouseenter with intent". Your function receives the same "this" and "event" objects as it would from jQuery's hover method. If the "over" function is sent alone (without "out") then it will be used in both cases like the handlerInOut param.&lt;/p&gt;
  
              &lt;h3&gt;out:&lt;/h3&gt;
              &lt;p&gt;The handlerOut function you'd like to call on "mouseleave after timeout". Your function receives the same "this" and "event" objects as it would from jQuery's hover method. Note, hoverIntent will only call the "out" function if the "over" function has been called.&lt;/p&gt;
  
              &lt;h3&gt;timeout:&lt;/h3&gt;
              &lt;p&gt;A simple delay, in milliseconds, before the "out" function is called. If the user mouses back over the element before the timeout has expired the "out" function will not be called (nor will the "over" function be called). This is primarily to protect against sloppy/human mousing trajectories that temporarily (and unintentionally) take the user off of the target element... giving them time to return. &lt;em&gt;Default timeout: 0&lt;/em&gt;&lt;/p&gt;
  
              &lt;h3&gt;selector:&lt;/h3&gt;
              &lt;p&gt;A selector string for event delegation. Used to filter the descendants of the selected elements that trigger the event. If the selector is null or omitted, the event is always triggered when it reaches the selected element. Read &lt;a href="http://api.jquery.com/on/#direct-and-delegated-events"&gt;jQuery's API Documentation for the .on() method&lt;/a&gt; for more information.&lt;/p&gt;
  
  
              &lt;h2&gt;Advanced Configuration Options&lt;/h2&gt;
              &lt;p&gt;Modify these if you are brave, test tirelessly, and completely understand what you are doing. When choosing the default settings for hoverIntent I tried to find the best possible balance between responsiveness and frequency of false positives.&lt;/p&gt;
  
              &lt;h3&gt;sensitivity:&lt;/h3&gt;
              &lt;p&gt;If the mouse travels fewer than this number of pixels between polling intervals, then the "over" function will be called. With the minimum sensitivity threshold of 1, the mouse must not move between polling intervals. With higher sensitivity thresholds you are more likely to receive a false positive. Note that hoverIntent r7 and earlier perform this comparison using rectilinear distance, whereas more recent versions compare against euclidean (straight-line) distance for better accuracy and intuition. If you are upgrading from an older version, you may want to verify that the desired behavior is preserved. &lt;em&gt;Default sensitivity: 6&lt;/em&gt;&lt;/p&gt;
  
              &lt;h3&gt;interval:&lt;/h3&gt;
              &lt;p&gt;The number of milliseconds hoverIntent waits between reading/comparing mouse coordinates. When the user's mouse first enters the element its coordinates are recorded. The soonest the "over" function can be called is after a single polling interval. Setting the polling interval higher will increase the delay before the first possible "over" call, but also increases the time to the next point of comparison. &lt;em&gt;Default interval: 100&lt;/em&gt;&lt;/p&gt;
  
  
              &lt;h2 id="defects"&gt;Known Defects&lt;/h2&gt;
              &lt;p&gt;hoverIntent r5 suffers from &lt;a href="http://code.google.com/p/chromium/issues/detail?id=68629"&gt;a defect in Google Chrome that improperly triggers mouseout when entering a child input[type="text"] element&lt;/a&gt;. hoverIntent r6 uses the same mouseenter/mouseleave special events as jQuery's built-in hover, and jQuery 1.5.1 patched this issue. Thanks to Colin Stuart for tipping me off about this and for providing isolated code to demonstrate/test.&lt;/p&gt;
              &lt;p id="chrome9defect" style="background:#eee;margin-bottom:1em;"&gt;This page uses jQuery 1.9.1 and hoverIntent r7, so when your cursor goes over the text input nothing should change (it should continue to read "enter parent" because you are still over this paragraph). &lt;br/&gt;&lt;input type="text" value=""/&gt;&lt;br/&gt; However, if you were using Google Chrome and if this page were using an older version of jQuery or hoverIntent, moving the cursor over the text input would improperly trigger the mouseout event, and the value would change to "leave parent".&lt;/p&gt;
              &lt;script type="text/javascript"&gt;
                  function enter(e){$("input", e.target).val("enter parent");}
                  function leave(e){$("input", e.target).val("leave parent");}
                  $("#chrome9defect").hoverIntent(enter,leave);
              &lt;/script&gt;
              &lt;p&gt;If you place an element flush against the edge of the browser chrome, sometimes Internet Explorer does not trigger a "mouseleave" event if your cursor leaves the element/browser in that direction. hoverIntent cannot correct for this.&lt;/p&gt;
              &lt;p&gt;Please email me &lt;strong&gt;brian(at)cherne(dot)net&lt;/strong&gt; if you have questions or would like to notify me of any defects.&lt;/p&gt;
  
              &lt;h2&gt;Release History&lt;/h2&gt;
              &lt;ul&gt;
                  &lt;li&gt;&lt;a href="jquery.hoverIntent-1.8.0.js" title="jQuery hoverIntent r8 (v1.8.0)"&gt;v1.8.0&lt;/a&gt; = (2014) Changed to &lt;a href="http://semver.org"&gt;Semantic Versioning&lt;/a&gt; (from r8 to v1.8.0). Removed &lt;a href="https://en.wikipedia.org/wiki/Zero-width_no-break_space"&gt;U+FEFF character&lt;/a&gt; from beginning of JS file. Removed stray "jQuery" in favor of "$" for noConflict situations. Changed measurements to use euclidean (instead of rectilinear) distance. Thanks to github community for patches, suggestions, and fixes!&lt;/li&gt;
                  &lt;li&gt;&lt;a href="jquery.hoverIntent-1.7.0.js" title="jQuery hoverIntent r7 (v1.7.0)"&gt;r7&lt;/a&gt; = (2013) Added event delegation via "selector" param/property. Added namespaced events for better isolation. Added handlerInOut support.&lt;/li&gt;
                  &lt;li&gt;&lt;a href="jquery.hoverIntent-1.6.0.js" title="jQuery hoverIntent r6 (v1.6.0)"&gt;r6&lt;/a&gt; = (2011) Identical to r5 except that the Google Chrome defect is fixed once you upgrade to jQuery 1.5.1 (or later).&lt;/li&gt;
                  &lt;li&gt;&lt;a href="jquery.hoverIntent-1.5.0.js" title="jQuery hoverIntent r5 (v1.5.0)"&gt;r5&lt;/a&gt; = (2007) Added state to prevent unmatched function calls. This and previous releases suffer from &lt;a href="http://code.google.com/p/chromium/issues/detail?id=68861"&gt;a defect in Google Chrome that improperly triggers mouseout when entering a child input[type=text] element&lt;/a&gt;.&lt;/li&gt;
                  &lt;li&gt;r4 = Fixed polling interval timing issue (now uses a self-calling timeout to avoid interval irregularities).&lt;/li&gt;
                  &lt;li&gt;r3 = Developer-only release for debugging.&lt;/li&gt;
                  &lt;li&gt;r2 = Added timeout and interval references to DOM object -- keeps timers separate from each other. Added configurable options. Added timeout option to delay onMouseLeave function call. Fixed two-interval mouseOver bug (now setting pX and pY onMouseEnter instead of hardcoded value).&lt;/li&gt;
                  &lt;li&gt;r1 = Initial release to jQuery discussion forum for feedback.&lt;/li&gt;
              &lt;/ul&gt;
  
          &lt;/div&gt;&lt;!-- close #main --&gt;
  
      &lt;/div&gt;&lt;!-- close #pageContent --&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p></p>
  
  <p>Can someone explain this? Document ready is "too fast" while serving local files or what? How to fix that, at least locally? :> Thank you!</p>