For example:
function change()
{
document.getElementById('identification').href = "http://www.stackoverflow.com";
}
The associated HTML (the important bit)
<a href="#" id="identification">Stack Overflow</a>
<input type=button value="Change" onclick="change()" />
This will change the href in my tag to http://www.stackoverflow.com, but say I wanted to do this from a different HTML file? The JavaScript would be in the tag of the other file, but would edit the content of the first. Is this possible? If so, how?
The HTML DOM allows JavaScript to change the content of HTML elements.
What you need to do is to turn your object into JSON string and save it like that to localStorage. And then when you want to use it on some other page you can transform it back to regular object. Here is how you turn object to string: JSON.
A . js file is an ordinary text file that stores your JavaScript scripts. You can store one or more of your JavaScript scripts in a single . js file and access them from multiple HTML pages.
Javascript lives only for the life of a particular page so you can't have code in one file modify another, as yet unloaded file.
Depending upon what you want the user experience to be, there are some options:
?show=true
. When you load the second page, request that page by appending the ?show=true
(or whatever you make up) to the end of the URL. When the second page loads, it can examine the query parameters on it's URL and decide how to modify itself. This is the simplest way of passing temporary arguments from one page to the next page.Note: the browser tries to prevent page modifications when the two pages do not have the same origin (e.g. same domain). See a description of the same origin policy. So, if your question pertains to pages on different domains, then you will need to find a different way to solve your problem. Things like add-ons can sometimes get around the same-origin policy, but regular page javascript cannot for numerous security reasons.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With