views:

63

answers:

4

Hi Guys,
How can i prevent links on click event before jQuery is loaded?
The reason is i have few links that make AJAX calls through jQuery ajax functions and if user click them before jQuery framework is loaded the browser cant trigger jQuery ajax function and will follow links href="..." Thanks.

Edit: Can i use this?

<head>
...
  <script type="text/javascript">
   window.onload = prevCl();
   function prevCl() {
    var links = document.links[];
    links.onclick = check();
   }
   function check() {
    if (typeof jQuery == 'undefined') { 
     return false;
    }
   }
  </script>
</head>
+2  A: 
T.J. Crowder
@T.J. Crowder: Thanks but its a huge DOM manipulation and have own issues about SEO and page loading speed. :)
Ali
@Ali: Good point about the SEO. I was adding another option as you pointed that out.
T.J. Crowder
@T.J. Crowder: thanks T. J. but i have feeeeeeeeeeew links, i think javascript can do it but im not good at javascript without frameworks i tried to write a function but didnt works. (can u look this function in my question? i edited that)
Ali
@Ali: No, you can't use `window.onload` for this. `window.onload` happens very, very late in the loading process (well after `jQuery.ready` does, for instance). I've added an Option 3, but I would say that Option 2 was probably your best bet.
T.J. Crowder
@T.J. Crowder: Thanks for the attention friend. :)
Ali
+1  A: 

I haven't tried it, but in theory, add an event handler without jquery to all a elements that prevents the default action (opening the link) as long as jquery is undfinied?

getElementsByTagNames('a').addEventListener('click',function (event) {
  if (typeof jQuery == 'undefined') { 
    event.preventDeafault();
  }
},false)
Hannes
+1  A: 
Sean Hogan
**Doh!** Of course, catching the clicks at the document level while the page is being rendered is the way to do this. I've taken the liberty of adding a complete, live example (and using `attachEvent` / `addEventListener` rather than `onclick`).
T.J. Crowder
A: 

Thanks to all specially Sean Hogan, I'm Ali. I dont know why i cant log in as user Ali again?!

I use Sean Hogan first code and It works for me on Firefox 3.6 and Internet Explorer 6+.

Live URL is: Sanetenaft FC Website a football club in Irans Premier League.

Ali