I have a requirement that I need to create a link to open a form in a new window and then when I click the same link it will focus on the existing window again.
this works well with the following code
<input type="button" value="Click" onclick = "return OpenWindow();"/>
<script>
var win = null;
function OpenWindow()
{
if (win == null || win.closed)
{
win = window.open('http://localhost/Conditions.aspx', 'Condition');
}
win.focus();
return false;
}
</script>
However, I experienced a problem, when I switch to different page and come back to the page which has the link. the variable win will not retain the previous reference.
If I click the link again, it will refresh the existing window (which is not what I want!) and then focus on it.
Is there any way that I can keep variable reference? or does anyone know how to solve this problem?
-
do you have control over the child window?Ibu– Ibu2012年10月12日 01:24:06 +00:00Commented Oct 12, 2012 at 1:24
-
yes, I do. The child window is a form which is for the user to keep as a separate window while he/she can navigate the main page to anywhere else.OKEEngine– OKEEngine2012年10月12日 01:31:02 +00:00Commented Oct 12, 2012 at 1:31
2 Answers 2
I had the same issue and was resolved on Window.open only if the window is not open
If you basically want the window focused instead of refreshed when the link is clicked, even if the parent window has been closed, re-opened, or changed, this will do the trick.
I was about to resort to using cookies.
1 Comment
Chia, your problem is that JS doesn't persist across pages. HTML is stateless, so the JS that sits on top also needs to "forget" what it did on page-1, after you move on to page-2 and page-3.
There are ways of storing strings and numbers, and retrieving them on other pages, but that's not what you're looking for.
And to that end, there's nothing you can really do, with your current setup.
There are different ways of allowing you to keep the child reference (do main-page navigation inside of an iFrame in the main page... if you really, really have to... or AJAX in the page changes, for people with capable browsers, and use old-fashioned navigation for browsers with worse JS engines).
But it's not going to be possible for you to open window2, click on a link which points window1 at page3, and still have page3 have a reference to window2.