user2638180
user2638180

Reputation: 1023

How to open a url in an already existing browser tab?

I'd like to do the following in my application via javascript.

1) Press a button from a web page that opens a popup.

2) Select some options in the popup and accept them.

3) Change the content of what is in the first web page according to what has been selected in the popup and change it. A GET Request is acceptable, and if possible popup with the selected options should again be on top, either by putting it on top or by opening a new one with the same options chosen.

I think javascript must have some way of saving the name of the browser tabs your are opening and later, if you want, open a new URL, or put one over other but I cannot find them. The window.open options doesn't look like they can do this.

Any idea on how to achieve this thing? Thanks for your time.

Upvotes: 1

Views: 1034

Answers (1)

Brian
Brian

Reputation: 1553

You can try the window.opener method in the popup to access the main page. High level details here: http://www.w3schools.com/jsref/prop_win_opener.asp, but if you Google it there's examples.

We did this for a project and it works, with a few caveats:

  1. Cross domain basically doesn't work, if you have multiple domains you have to get more creative.
  2. Be careful cross browser as well, we had to add some custom javascript to handle

Basically, what you need to do on your main page is define a global javascript method that does what you want it to (it can take parameters). Then, on your popup, you can call it with window.opener.MethodNameHere();

Theoretically, if you do need to handle cross browser, you can try using postmessage, which I believe is only supported in html5 natively (there are plugins for html4), but it would probably be tricky getting it right in this circumstance and I'm not sure how to do it off the top of my head.

Upvotes: 1

Related Questions