views:

26

answers:

4

Hi guys

Small prob i'm using the following line of code

customerName = window.opener.form2.custName.value;

The problem is that I'm no longer opening the page in a new window, I'm opening it in the same window and thus replacing the older page.

Is there anyway for me to get the same information?

Thanks

+1  A: 

Pass it as a paratemer in the query string to the new page.

If you would open the new page as, say: <a href="newPage.html">..., pass the details as <a href="newPage.html?custName="+document.form2.custName.value>

As an alternative you can use cookies to store the data from page 1 before you navigate away and retrieve it in page 2.

Nivas
how would i then set the customerName var in the new page to be equal to the parameter passed in the query string?
Uncle
You need to parse the query string. See http://blog.falafel.com/Blogs/AdamAnderson/07-12-17/Parse_a_Query_String_in_JavaScript.aspx
Nivas
Remember, the query string will snow up in the URL, and this is perhaps what you want. If not, use cookies: http://www.w3schools.com/JS/js_cookies.asp
Nivas
Great answer, does exactly what i need!
Uncle
A: 

If you aren't opening a new window, window.opener won't be available.

You could try passing the value of custName on the query string e.g. http://example.com/somepage.htm?custName=&lt;yourValueHere&gt;.

Its relatively easy to get query string values in javascript, try using http://plugins.jquery.com/project/query-object for example.

Otherwise, set a cookie and retrieve its value on the 2nd page.

James Simm
A: 

Since you're replacing the older page with the new one, the old data is gone. Consider POSTing the first page's form into the second, e.g. (using ASP.NET):

<form id="form2" action="second_page.aspx" target="_self">
    <input type="text" id="custName" />
</form>

Then call form2.submit() instead of window.open(). In second_page.aspx, you can then use Request.Form to get custName's value.

Frédéric Hamidi
A: 

You need to pass the value / values you want on to the new page in some way. Some pointers:

  • Use a cookie to store the value in one page, then retrieve it in the next.
  • Pass it on as a parameter in the URL, or by submitting a form to the new page.
  • Store it in session, then retrieve it.

Of course, you may also wish to consider the nature of the data. If it is sensitive, you should use the session or form alternatives to ensure that info is not viewable from the outside (in a cookie, or in the browser's URL, for instance).

Ioannis Karadimas