shay benily
shay benily

Reputation: 39

XMLHttpRequest - Change Value with getElementById

I want the button on the second page to change the value of the button in the first page,this is my code:

FILE 1: Index.html(page one):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Eran Exercise</title>
<link rel="stylesheet" type="text/css" href="design.css" />
<script src="new_file.js"></script>

</head>
<body>

    <div class="wrapper">

        <form> 
<input type="button" id="id200" value="New Window!" onClick="window.open('page2.html','mywindow','width=400,height=200')"> 

</form> 

                    </div>


</body>
</html>

FILE 2:new_file.js(the js file)

function changeLink()
{
var someOtherName="after click";

xmlhttp=new XMLHttpRequest();

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("id200").value = someOtherName;
}
 }
xmlhttp.open("GET","index.php");
xmlhttp.send();
}

FILE 3:page2.html (page two)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
  "http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
<head>
<script src="new_file.js"></script>
 </head>

<body>
<input type="button" id="id100" onclick="changeLink()" value="Change link">
</body>
</html> 

Thanks!

Upvotes: 0

Views: 1605

Answers (2)

KOLANICH
KOLANICH

Reputation: 3072

  1. previous answer +1

    but it will work only on the same domain or if the server sends special header

  2. you can use cookies or local storage

    save in it state on the second page and get it on the first page

    also you can use php script and get/save the state from/to there

  3. you had missed the third parameter at xhr.open

    and never use sincronious requests

Upvotes: 0

scessor
scessor

Reputation: 16115

Replace

document.getElementById("id200").value = someOtherName;

by

window.opener.document.getElementById("id200").value = someOtherName;

Upvotes: 1

Related Questions