简体   繁体   中英

Perform an action after clicking on the link

I'm new to JavaScript, I need to perform an action after clicking on the link. For example, we have two pages A and B On page A, I perform a JavaScript action (filling in the text field and clicking on the button), there is a transition to page B. Here, too, you need to perform some JavaScript action, such as clicking on a link. Here is an example that only works on page A, when the transition occurs, the rest of the code seems to be erased, this is not a fact, but my guesses

document.getElementById('story').value='Титаник';
document.querySelector('body > div.wrapper > div.header > div.header44 > div.search_panel > span > form > button').click();
document.getElementsByTagName('a')[1].click();

Or another example, Follow the link and fill in the field there, which also does not work

window.location.href='http://www.ganjawars.ru/login.php'
function F() {
document.getElementsByName('login')[0].value = 'login';
document.getElementsByName('pass')[0].value = 'password';
}
 onload= F ()

How do I perform an action after clicking on a link?

Unfortunately, JavaScript does not work like this. The variables that you store on page A, will be gone, when you navigate to page B.

Possible solutions:

  • One-page website
  • jQuery post the variables to another page.
  • PHP Sessions
  • Cookies

Session storage is actually the easiest solution, providing that you only want to navigate to a different page on the same website. This breaks when you close the browser or navigate to a new website.

story = document.getElementById('story').value='Титаник';
sessionStorage.setItem("story", story);
document.getElementsByTagName('a')[1].click();

and on the other page:

story = sessionStorage.getItem("story");

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