0

I have two html pages one is parent.htm and other is child.html where i have given href in parent.htm' tochild.html, here i have to access the elements ofparent.htminchild.html`, here are my two files

parent.htm

<!DOCTYPE html>
<html>


<body>
<form>
<input id=text1 type=text name="test2">
<a href="child.html" target="_self">Open kid</a>
</form>
</body>
</html>

child.html

<html>
<script type="text/javascript">
function parent(){
    //here i want to access values from parent.htm page, but does not work
var value = parent.getElementById('text1').value;
    var value2=top.getElementById('text1').value;
alert(value);
}
</script>
<body onload="parent()">
<form>
    </form>
</body>
</html>

Thanks in advance

4
  • Check this may be help you stackoverflow.com/questions/13151634/… Commented May 29, 2013 at 12:35
  • Any reason you couldn't just set cookies on the client and read them on the second page? Commented May 29, 2013 at 12:40
  • the requirement is to just use JS @Tim Commented May 29, 2013 at 12:41
  • You could set and read cookies with Javascript. You'd save the cookies when the link is clicked and read them into the second page on page load. Commented May 29, 2013 at 12:49

5 Answers 5

2

Probably the easiest way to do what you want is through a cookie. It's a small text file stored on the client that can persist values across pages. If you don't specify an expiration date, the cookie will expire when the user closes their browser.

Cookie tutorial

Edit

Something else you could do is use Javascript to submit the form by clicking on the link. I think it's formname.submit() - which would allow you to read the values out of the form post. (Though it would be a little more work than just reading the cookie)

If you're only passing one or two fields I'd use the cookie. More than that you may want to consider submitting the form through Javascript.

Sign up to request clarification or add additional context in comments.

Comments

1

First change the following:-

<a href="child.html" target="_self">Open kid</a>

to :-

<a href="child.html" target="_blank">Open kid</a>

target="_self" means that the child page will open in the same parent window, effectively destroying the parent and its code.

Afterwards, access the parent's form textbox element with:-

var parentTextBox = window.opener.document.getElementById('text1');

2 Comments

My requirement is to open the child.html in same page @Moje
@MaheshVarma have you considered asynchronously loading the child's html UI and displaying it after hiding the parent's UI?
1

I think you cannot do that, at least the way you are trying.

Once you clicked on "Open kid", your current page will be replaced by the new one, so you can't access that attribute.

You should be able to get around this by using cookies, passing the needed values in the url or with Web Storage.

Comments

1

You should be able to user window.opener to grab a reference to the parent window.

var parent = window.opener

I see -- you cannot access DOM elements from a previous page via javascript (AFAIK).

This is traditionally handled with HTTP post variables, passing the form variables collection to the subsequent page via some back-end procedures.

Or (as Moje notes) open the page in a new window so you can access the parent with window.opener

1 Comment

But I'm not opening a new window by using 'window.open()', it's just href link @faust
0

parent.htm

<!DOCTYPE html>
<html>
  <body>
    <form method="GET" id="myform" action="child.html">
      <input id=text1 type=text name="test2">
      <a href="child.html" target="_self" onclick="document.getElementById('myform').submit();">Open kid</a>
    </form>
  </body>
</html>

child.html

<!DOCTYPE html>
<html>
<body onload="alert(window.location.toString().split('?')[1].split('=')[1])">

</body>
</html>

Might contain some typo, but the idea is to submit form by GET to the child and then read GET parameters from window.location after child.html is loaded.

3 Comments

Good idea, here's a fiddle showing something similar in action.
Thanks @ShadowWizard - my sample code is rather dirty, but we don't know what is he going to do with grabbed values. So I just made this ugly one-line alert :)
Yep that's the general idea, my fiddle contains more generic approach.

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.