0

I have an html page:

<html>
<head>
<script type="text/javascript">
function myfunct()
{
window.location='test.html';
document.getElementById("message")="hello";
}
</script>
</head>
<body>
<input type="button" value="click" onclick="myfunct()"/>
</body>
</html>

test.html:

<html><head></head>
<body>
<div id="message">Some text</div>
</body>
</html>

The problem is that window.location is working fine but the div is not getting updated to "hello".

4 Answers 4

2

You can't assign a string to an element, you have to use the innerHTML property. Also, as the element is in the page that you open, you have to have the code in that page instead.

test.html:

<html>
<head>
<script type="text/javascript">

window.onload = function() {
  document.getElementById("message").innerHTML = "hello";
}

</script>

</head>
<body>
<div id="message">Some text</div>
</body>
</html>

If you want to send the message along from the first page, you have to send it as data to the new page, so that it can display it:

In the first page:

window.location.href = 'test.html?hello';

In test.html:

window.onload = function() {
  document.getElementById("message").innerHTML = window.location.search.substr(1);
}
Sign up to request clarification or add additional context in comments.

3 Comments

The point is test.html already has some text, so when i use window.location to switch to test.html, i want to update the text in test.html
@user475685: Yes, that's what the code does. The first example above shows that you have to have the code to update the text in the same page where the element is. The second example shows how you can control what text to show from the first page.
@user475685: My bad, you have to use window.location.search to get the query string, not window.location.hash. I corrected the code above.
1

you have to write

function myfunct() { document.getElementById("message")="hello"; }

in test.html page...

and call this function on page Load event of test.html

1 Comment

The point is test.html already has some text, so when i use window.location to switch to test.html, i want to update the text in test.html
1

you have to write in test.htm

document.getElementById("message")="hello";

not in your html

1 Comment

The point is test.html already has some text, so when i use window.location to switch to test.html, i want to update the text in test.html
1

The element you are trying to set to 'Hello World' does not exist on the page you're function is on. You can only access elements on your current page. When a page refresh happens it recreates the HTML DOM from the requested page. You cannot access another pages DOM directly.

1 Comment

That is not correct. Setting the window.location does not immediately load a new page, that happens when you exit from the function and return control to the browser. The rest of the function is still executed.

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.