I have a link which should open in a new tab, but if the tab is already open, just switch to it. I've tried with javascript, wnd = window.open() and than wnd.focus(), that works in Chrome 19, but not in FF 13 or IE 9. Here's the code I've written :
<script type="text/javascript"> var loadingTableWnd; function openOrSwitchToWindow(url){ if(loadingTableWnd == undefined) loadingTableWnd = window.open(url,'myFrame'); else loadingTableWnd.focus(); } </script> <a href='javascript:openOrSwitchToWindow("/");' >Loading Table</a>
Any idea how can I open or switch to from every browser?
EDIT: I need to open the link in a new tab, not a stand-alone window.
Different browsers behave differently for window.open() and focus(). For this code window.open('www.sample.com','mywindow').focus()
Fiddle to test with: http://jsfiddle.net/jaraics/pEG3j/
You shouldn't need any logic for something like this. By default, specifying the second parameter for window.open()
gives the window a "name", that the browser remembers. If you try to call window.open()
with the same name (after it's already been opened), it doesn't open a new window...but you might still need to call .focus()
on it. Try this:
var a = window.open(url, "name"); a.focus();
Those should be the only lines of code in your function, and you don't need the loadingTableWnd
variable...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With