Reputation: 385
Basically I want to have a button/link that opens google in a new tab, but if that tab is already open then just focus that tab. Is this possible?
Upvotes: 0
Views: 69
Reputation: 129069
Kind of. The closest thing I can think of would be something like this:
<a href="http://www.example.com/" target="example">Link</a>
When you click it the first time, it will open the link in a new tab. Subsequent clicks of the link will navigate the tab it opened the first time (even if that tab has since navigated to a different page). It will not change the page of a tab that was opened without using the link.
Upvotes: 3
Reputation: 499
Yes, you can and it is quite simple task - but remember that nowadays some browsers (With addons) will either block a new window you create or will not recognize it as "pop-up" that can be controled in some internal JavaScript.
Proper method is:
<script>
var myTab;
function openIt()
{
myTab=window.open('','','width=200,height=100');
myTab.document.write("<p>Some kind of inner text</p>");
myTab.blur(); // this will un-activate opened window/tab
}
function activateIt()
{
myTab.focus();
}
</script>
<input type="button" value="Open" onclick="openIt()">
<input type="button" value="Activate" onclick="activateIt()" />
To controll it see documentation for window.open() method.
Upvotes: 1