0

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?

asked Oct 12, 2012 at 1:13
2
  • do you have control over the child window? Commented 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. Commented Oct 12, 2012 at 1:31

2 Answers 2

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.

answered Nov 16, 2012 at 22:41
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this was what I intended to do.
2

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.

answered Oct 12, 2012 at 2:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.