views:

315

answers:

2

Hi, I have an issue with Cross-Browser native events vs CallBack events.

I have an HTML link "Click Me" with a given href="". On dom:loaded I attach a function to this link (to do Ajax Stuff).

JavaScript code is loaded at the end of the page to follow YSlow Recommandation.

Issue:

If you load this page really quickly (pressing F5) then click on link then

  • the alert() is not called
  • the link is followed (reloading the page)

It happens when the server lags. In fact the page has not finished loading and the browser execute the code.

Demo:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html>
  <head>
  </head>
  <body>

    <a href="#toolate" id="action">Click Me</a>

    <!-- According to YSlow Recommandation load at the bottom -->
    <script src="../js/lib/prototype.js" type="text/javascript" language="JavaScript"></script>
    <script>
      /* <![CDATA[ */
      document.observe('dom:loaded', function() { 
        $('action').observe('click', function(event){ alert("click"); Event.stop(event); });
      });
      /* ]]> */
    </script>
  </body>
</html>

Turn Around:

A turn around is to add onClick="return false;":

<a href="#toolate" id="action" onClick="return false;">Click Me</a>

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 onclick on all <a href="">

Best Regards

A: 

You could look into this:

JQuery has a handy little function that launches your javascript as soon as the Document Object Model is ready… which happens before the page has finished loading.

$(document).ready(function(){   //
 Your code here... 
});

via

You could also put a big disabled div in front of everything while the page is loading to forbid clicking, but I wouldn't recommend it.

Not sure if I got your question right, let me know if I didn't

marcgg
My function might need to work with the DOM that should be ready. On the Exemple, if an onClick on the <A> will not be called if user click quickly. Javascript Engine is not ready I thinkWe tried the big DIV shield technique but it's a little bit cludge ;)I thought it would be a common problem but may be there is no "clean" solution.
Jean-Philippe Encausse
A: 

It seems like you have things pretty well in hand, and all you're looking to do is create some links which do nothing in areas where graceful degradation is not possible.

In these cases I suggest a link with the following format:

<a href="javascript:void(0);">Linktext</a>

This link should functionally do absolutely nothing with javascript enabled or disabled.

Important Notes

  1. The number zero in the void is absolutely necessary. Internet Explorer will complain otherwise.
  2. The use of "javascript:void(0);" is strongly discouraged by Microsoft because odd things can happen. To avoid the majority of the bugs, do not wrap anything but text in your void(0) links.
  3. Before you make a decision make sure to carefully consider if adding "javascript:void(0);" is really a better solution than adding an onclick which returns false.

On removing the href attribute:

Removing the href attribute from a link is valid XHTML. Despite being valid XHTML, your link will lose its automatic link styling. No more underline, no more colors, no more hover, and no more automatic color change if the link is visited.

You can't fix the lack of styles with CSS either due to the spotty support of :hover in Internet Explorer.

coderjoe
Thanks for your answer. It is good to know that javascript:void(0); should be better that "return false". But I was looking for something more global. Can I remove the href like you said in the comment ? is it valid XHTML ?
Jean-Philippe Encausse
Yes, you can remove the href attribute and it is valid html. However, your link ceases to be a link and you lose some functionality because of that. I've updated my answer with specifics.
coderjoe