简体   繁体   中英

How can I use a html Anchor to link to page and then execute the javascript function on that page?

I've written a script that changes the content of a div in the main part of the html, using:

document.getElementById("id").innerHTML = "the stuff I want to put in the main box";

and an array storing the content so I call changetxt(1) it will change the innerhtml to 'content[1]'etc.)

What I now want to do is link from another page including the changed content, so for example I link to the 'where we meet' content section, (content[2]) but when I link to the html it obviously just shows the default value, I was wondering if there is some way I can link to the content via an Anchor on another page such as -

href="index.html-javascript:changetxt(2); 

I've not found anything on google or this site explaining how to do so.

many thanks

why not use hash tags?

The link:

index.html#2

The javascript in the new page (index.html):

if(window.location.hash) {
  var num = Number(window.location.hash.substr(1));

  if(num > 0) {
    changetxt(num);
  }
}

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