Alex Man
Alex Man

Reputation: 4886

reloading new webpage content in the same opened tab through javascript

I have created two hyperlink in which when I click the hyperlink, it should open a new tab in the browser with the corresponding link webpage content, I have done that by using window.open feature,Its working fine, But the problem is that say when I click the hyperlink for the very first time it will open up a new tab, Again when I click the same or the second hyperlink it is opening another tab within the browser, instead If the current opened tab is still opened I need it be reloaded with that tab itself, If none of the tabs were opened then it should open a fresh new tab.

Is this can be done through JavaScript

My Code is as given below

JSFiddle

html

<a href="javascript:void(0);" onclick="openTab('http://stackoverflow.com')">StackOverflow</a>

<a href="javascript:void(0);" onclick="openTab('https://github.com')">GitHub</a>

script

function openTab(link) {
    window.mypopup = window.open(link);
}

Upvotes: 0

Views: 1588

Answers (2)

bviale
bviale

Reputation: 5375

Edit: I changed my whole answer, since I saw there is a cleaner way you will prefer :

var w;

function openTab(link) {
if (typeof(w) === 'undefined')
    w = window.open(link);
else {
    w.location.href=link;
}

}

See this topic where this problem has already been solved.

Cheers

Upvotes: 2

Tolokoban
Tolokoban

Reputation: 2339

You can use TARGET to use the same tab for different URLs.

http://jsfiddle.net/07hh3z2n/2/

This is the second argument of window.open.

window.open("http://en.wikipedia.org", "MyTab");

Upvotes: 1

Related Questions