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?
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.