views:

49

answers:

5

I have a jQuery click event which runs when an element is clicked. Is it possible to get this to run once when the page is loaded?

+4  A: 
$(document).ready(function(){
//do something here
});
akellehe
I can't do that because the jQuery is in a js file which is included on all pages, so I do not want it to run on all pages, only 1 specific page.
oshirowanen
then you should include this code on only in the head of that page
akellehe
Is that the only way? Can I not execute a javascript function within the body tag of the page in question?
oshirowanen
Since the do something function is available to all pages make a page specific js file and put the call to the do something function in the document ready function of that js file.
Paul Speranza
You can, I believe it is best practice, though, to load the scripts that can load before the body before the body. Someone please correct me here if I'm mistaken.
akellehe
@Paul Speranza: I believe to optimize performance it's best to call as few external javascript files as possible, no? If this is to be run on only one page it seems to make the most sense to simply include the code on that page. -- EDIT: Unless of course you swap out the entire js file for just that page; then there is less, if any performance hit.
akellehe
@akellhe Then the function he is trying to call should only be in the page's js file. He needs to move it. Sometimes you can't avoid this and the page specific js file would be small and cached. I think the clean separation is worth it.
Paul Speranza
@Paul Speranza that's a very valid point.
akellehe
@akellhe I usually end up with a js file for each page. When you start tying in scripts for various plugins you will have settings for the plugins specific to each page. Imagine trying to have a huge mess of javascript all in one js file. Ugly. Take a look at the setup for something like jqGrid or the JQuery UI components. I always evaluate each problem separately to decide on the best way to go.
Paul Speranza
I've tried separating that function into another js file. It now loads on page load, but I cannot call that function from a click event...
oshirowanen
@oshirowanen If it is in scope you should be able to call it. Post your code.
Paul Speranza
A: 

You can do like another poster's suggestion or try this (stolen from here):

<script type="text/javascript" 
        src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
<script type="text/javascript">
  // You may specify partial version numbers, such as "1" or "1.3",
  //  with the same result. Doing so will automatically load the 
  //  latest version matching that partial revision pattern 
  //  (e.g. 1.3 would load 1.3.2 today and 1 would load 1.4.2).
  google.load("jquery", "1.4.2");

  google.setOnLoadCallback(function() {
    // Place init code here instead of $(document).ready()
  });
</script>
Lee Sy En
+1  A: 
$(document).ready(function(){

  $('#yourelement').bind('click', function() {
     /* set handler onclick */

  })
  /* trigger ('run once') on domready */
  .trigger('click'); 


});
Fabrizio Calderan
+1  A: 

sure, just issue a click() like:

$(document).load(function(){

   $('#someelement').click(function(){
      alert("some element clicked");
   });

   $('#someelement').click();
   // or $('#someelement').trigger('click');
});

working example here --> http://jsfiddle.net/ZXyJH/

Jamiec
How do I put that into the <body> tag of the page which should trigger the click?
oshirowanen
you dont. You put it in the head inside a script tag.
Jamiec
A: 

As I mentioned in the comments, especially if the code is used in one page, break it out into a js file for that page. Why carry around a bunch of javascript in every page. Imagine doing that through the entire site.

If you need it in more that one page break it out into a function either in your main js file or another js file that is used by one or more pages. Set up a call to it from your click event and you will also be able to call it from anywhere else, such as from a doc ready function in a page specific js file.

Paul Speranza
Why is it that most here: http://stackoverflow.com/questions/3829801/what-is-faster-and-why recommend sticking all javascript into a single file? The example they gave was jquery.
oshirowanen
The least amount of files is preferreable but can you imagine trying to maintain a js file with thousands of lines of code for every bit of code for an entire site? Even with a tool like Visual Studio that has an add on for collapsing funcitons I still prefere multiple files. You have to decide on your specific needs.
Paul Speranza
Check out this Best Practices for Speeding Up Your Web Site from yahoo link. http://developer.yahoo.com/performance/rules.html See the section "Make JavaScript and CSS External" and note at the beginning of the page they do discuss combining files.
Paul Speranza