Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use JavaScript in one document to change HTML in another?

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?

like image 425
please delete me Avatar asked Sep 21 '11 01:09

please delete me


People also ask

Does JavaScript can change HTML content?

The HTML DOM allows JavaScript to change the content of HTML elements.

How do I transfer data from one HTML page to another using JavaScript?

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.

Can I use one js file for two HTML files?

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.


1 Answers

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:

  1. While on the first page, use some javascript to set a cookie and then when the second page loads, read that cookie and have the javascript in the second page adapt based on the cookie value. Cookies can be shared between pages on the same domain.
  2. While on the first page, use some javascript to create a query parameter (those things after the ? in a URL that look like this ?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.
  3. Load the second page into an iframe (embedded into the first page) and when it's loads, your javascript can modify it (if it is served from the same domain as your main page).
  4. Load the second page into your first page, actually inserting it into the original page either appending it or replacing some of your existing content. Then, the first page's javascript can modify the HTML from the second page once it has been inserted.
  5. Open a new window with the new page in it. If it's on the same domain as you, then your javascript can reach into that new page and modify it.

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.

like image 181
jfriend00 Avatar answered Sep 22 '22 17:09

jfriend00