Jump to content

onload event


S Murder

Recommended Posts

I was wondering, does anybody know if the onload event will fire when you click an anchor in the same page?
The load event fires when the document loads in the browser. If your page is called "index.html" and you have a hyperlink on that page that references "index.html", then, yes, when you click on that link, the page will reload, and the load even will fire.
Link to comment
Share on other sites

The load event fires when the document loads in the browser. If your page is called "index.html" and you have a hyperlink on that page that references "index.html", then, yes, when you click on that link, the page will reload, and the load even will fire.
What I meant was, if I'm viewing index.html and click a link to index.html#info that goes to an anchor within the page (which means it won't reload), will an event fire?
Link to comment
Share on other sites

No, in this case the load event will not fire. Perhaps you could use the click event for the link?

<a href="#info" onclick="somefunction();">info</a>

Link to comment
Share on other sites

No, in this case the load event will not fire. Perhaps you could use the click event for the link?
<a href="#info" onclick="somefunction();">info</a>

I'm using anchors to pass variables to the URL without making the page reload (it's for an AJAX app) and I wanted to use regular links instead of JS so that if someone opens the link in a new tab or window or bookmarks the page it will still work. I guess using both a regular link and onclick event would work, so long as I have an onload function calling the same variable evaluation method that onclick does (for when the page is loaded).Do you know whether the page+URL would change first, or would the event fire first?
Link to comment
Share on other sites

Do you know whether the page+URL would change first, or would the event fire first?
The event happens first.Don't know if this applies to your situation or not, but you can stop the page+URL from changing by returning false in the onclick handler code:
<a href="#info" onclick="somefunction(); return false">Info</a>

In this example, only "somefunction()" would fire. The browser would not see the request to navigate to the #info anchor because we returned false out of the click handler. The way you might use this in your application could be like this:

<a href="#info" onclick="somefunction(this.href); return false">Info</a>

Then, rather than looking at the location.href or location.search to get the "#info", you can simply pass it into the function as a parameter.Hope this helps.

Link to comment
Share on other sites

The event happens first.Don't know if this applies to your situation or not, but you can stop the page+URL from changing by returning false in the onclick handler code:
<a href="#info" onclick="somefunction(); return false">Info</a>

In this example, only "somefunction()" would fire. The browser would not see the request to navigate to the #info anchor because we returned false out of the click handler. The way you might use this in your application could be like this:

<a href="#info" onclick="somefunction(this.href); return false">Info</a>

Then, rather than looking at the location.href or location.search to get the "#info", you can simply pass it into the function as a parameter.Hope this helps.

It does, thanks.There won't actually be an #info anchor, I'll just be putting the GET values after the # in the URL to keep the page from reloading, and upon initial page load or link click the GET values after the # will be changed and the page will be altered with AJAX based on the changed variables.I was also wondering, if I do href="java script:getURL()" where getURL() returns a URL string, will the URL be followed?
Link to comment
Share on other sites

was also wondering, if I do href="java script:getURL()" where getURL() returns a URL string, will the URL be followed?
Are you asking something like this....<script>function test() {// Return URL, will NOT work // return "http://www.google.com"// to redirect, this will work window.location="http://www.google.com"}</script><a href="java script:test();">Redirect URL</a>
Link to comment
Share on other sites

Are you asking something like this....<script>function test() {// Return URL, will NOT work // return "http://www.google.com"// to redirect, this will work window.location="http://www.google.com"}</script><a href="java script:test();">Redirect URL</a>
Yeah, I just realized that. It's cool though; I can just pass a reference of the anchor tag to the function and then modify the href attribute.
Link to comment
Share on other sites

I was wondering, is there anyway to detect a middle click with javascript? Will a middle click fire a regular onclick Event?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...