Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling browser native click before dom:loaded
    primarykey
    data
    text
    <p>I have an issue with Cross-Browser native events vs CallBack events.</p> <p>I have an HTML link "Click Me" with a given <code>href=""</code>. On <code>dom:loaded</code> I attach a function to this link (to do Ajax Stuff). </p> <p>JavaScript code is loaded at the end of the page to follow YSlow Recommandation.</p> <p><strong>Issue</strong>:</p> <p>If you load this page really quickly (pressing F5) then click on link then</p> <ul> <li>the <code>alert()</code> is not called </li> <li>the link is followed (reloading the page)</li> </ul> <p>It happens when the server lags. In fact the page has not finished loading and the browser execute the code.</p> <p><strong>Demo</strong>:</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;body&gt; &lt;a href="#toolate" id="action"&gt;Click Me&lt;/a&gt; &lt;!-- According to YSlow Recommandation load at the bottom --&gt; &lt;script src="../js/lib/prototype.js" type="text/javascript" language="JavaScript"&gt;&lt;/script&gt; &lt;script&gt; /* &lt;![CDATA[ */ document.observe('dom:loaded', function() { $('action').observe('click', function(event){ alert("click"); Event.stop(event); }); }); /* ]]&gt; */ &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>Turn Around</strong>:</p> <p>A turn around is to add <code>onClick="return false;"</code>:</p> <pre><code>&lt;a href="#toolate" id="action" onClick="return false;"&gt;Click Me&lt;/a&gt; </code></pre> <p>It works for lags but not for quick click. And I don't like this kind of turn around because my goal is to remove the <code>onclick</code> on all <code>&lt;a href=""&gt;</code></p> <p>Best Regards</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload