'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?
Solution 1:[1]
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");
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ned Hulton |
| Solution 2 | Ned Hulton |
