简体   繁体   中英

url link to activate a javascript function once the page has loaded

I have a page where various javascript functions shows different part of an array.

The code I use to select what information to display is:

<a href="#" onClick="x(ncode1);">
<a href="#" onClick="x(ncode2);">

Clicking the a tag will activate the function x(ncode). The page default loads to x(ncode1). When x(ncode2) is activated, the url naturally changes to site.com/#

If I want to make a link direct to the x(ncode2) display, I understand I need to link to the page and activate the javascript function once the page has loaded. How so I do this?

Based on Halcyon this code works:

If the page is site.com/#2 run function x(ncode2) after the page has loaded

window.onload = function() {
 if(window.location.hash == "#2"){
         x(ncode2);   
 }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM