Pregunta

I currently have a button which opens a new window in a new tab using window.open("newpage.html")...I use childVar = window.opener.parentGlobalVar in the child window to retrieve the values of some global variables from the parent window. Now I have been asked to open the new window in the same tab as the parent, and include a 'back' button to go back to the parent window.

So far, my attempts to do this have not worked, I've tried using _top or _self as arguments for window.open, window.location, and location.href... all of which fail to retrieve the needed variables from the parent; I assume these values are cleared when I open a new page in the same tab. Is there a way that I can store these values (client-side) or pass them to the new window, if it opens in the same tab?

¿Fue útil?

Solución 2

You could use localStorage to store data client-side.

localStorage.setItem("bar", foo);
var x = localStorage.getItem("bar");

Works kind of like cookies but there are also risks involved. I'd advice you to research it a little, starting for example here

You can basically store everything in it, for example your referring URL.

EDIT @comment:

to clear the whole storage you can use:

localStorage.clear();

and to clear a certain key/value pair: localStorage.removeItem(key) in terms of compatibility: caniuse.com is pretty decent.

Otros consejos

You can't do that.

Once your window (page) has been unloaded (when the tab navigates to the new page), it no longer exists.

Instead, you can pass the data in the querystring.

Could try the window.close("newpage.html") method.. this would close the "newpage.html"(back button) and return to the original page (then they would just click the link they used to open the "newpage.html" if the need to view the page or return to the tab is necessary..
Code would be similar to:

<p>Click to close and return to last page.</p>
<button onclick="myFunction()">close</button>

<script>

function myFunction()
{
window.close("newpage.html");
}
</script>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top