1

Consider an HTML page named page1.html with the following simple structure:

<html>
<title>Page1</title>
<body>
  <h1>Hello</h1>
</body>
</html>

Is it possible to change<h1> text on page1.html with JavaScript from page2.html?

If it’s possible, how can one achieve this?

7
  • Do you show page2.html? Commented Aug 25, 2018 at 8:17
  • Should the change be persistent (i.e. like editing Wikipedia, after you do the action on page2.html then the h1 on page1.html will be set to the new value for all that load the page after that point), or does it need to be dynamic and only to the person that currently has the page open (i.e. like Facebook chat, where you send a message to one user, and it appears on the other user's screen)? Commented Aug 25, 2018 at 8:26
  • actually I didn't provide any code for page2.html, but I was Curious that, is the problem feasible? and then how it could be implemented? Commented Aug 25, 2018 at 8:57
  • I do not want to make changes persistent but just change the needed value for current load. Commented Aug 25, 2018 at 8:59
  • What exactly are you trying to achieve here? Commented Aug 25, 2018 at 9:33

3 Answers 3

4

Assuming you serve both your HTMLS from the same origin (e.g. if you had two facebook tabs open), you could solve this by using the 'storage' event (Local Storage is shared between tabs), like this:

HTML1:

<script>
    localStorage.setItem('message', JSON.stringify({ 'myContent': 'i like pizza' }));`
     localStorage.removeItem('message');
</script>

HTML2

<script>
    $(window).on('storage', e => {
        var message = JSON.parse(e.originalEvent.newValue);
        $('#myH1').text(message.myContent);
    });
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

How is storage an event?
thanks to iuliu.net for the answer, but as i said, i do not aim to make any modification in page1.html
Yes. Using this example, the changes will not be persisted, they'll be there just for the current load. (don't be fooled by seeing localStorage here, it's nothing but a communication mechanism between the two pages)
2

I think the short answer you are looking for is "No".

You cannot modify one static page using another static page without a server between them.

Some solutions as mentioned from other pals can work, like using a local storage or stuffing a cookie -Only if they will run on the same browser-, but it will definitely require some changes in page1.html which is not you're looking for.

Comments

0

Since you commented that you don’t need it to be persistent, what you want is to create an illusion that you’re changing <h1> text on page1.html with JavaScript from page2.html.

You need two things:

  1. First, to change the actual text in <h1> use .innerText property with replace() method.

  2. To change the URL in the address bar from /page2.html to /page1.html use the History API with history.pushState() and history.replaceState() methods.

8 Comments

Btw, I’m still not entirely sure what you actually want to achieve.
can I perform number 1 in page2.html?
Since page2.html is virtual, then the answer is yes.
actually, i have an alarm system which generates alarms in a web page while i do not have access to the source code of the program, but i want to acknowledge those alarms by scripting on another page so the acknowledgement process can be done automatically by my script.
If I understood correctly, what you want can’t be done.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.