THRILLHOUSE
THRILLHOUSE

Reputation: 163

Open to new window or tab on click

I am trying to make different countries in this map (http://jsfiddle.net/hansenmc/EnqP3/) open to a new tab or window. I am able to open in the same window without an issue using this code.

     onRegionClick: function (element, code, region) {
        
        
        if(code == 'il'){
window.open("www.mywebsite.com", "_blank");
}

When I use this code it opens in the same window and a new window simultaneously.

    onRegionClick: function (element, code, region) {
        
        
        if(code == 'us'){
window.open = "www.mywebsite.com, _blank";
}

Any help getting different countries to open into their own window would be greatly appreciated. I have tried solutions from other threads but none seem to work in this scenario.

Upvotes: 0

Views: 2010

Answers (2)

CodeTheorist
CodeTheorist

Reputation: 1672

window.open is a function, so it should be called instead of assigned to.

Instead of:

window.open = "www.mywebsite.com, _blank";

It should be:

window.open("www.mywebsite.com", "_blank");

Also, if you want the function open the URL correctly, it should include the http(s):// prefix, otherwise it will open a relative path from the current windows location in some browsers.

The full code in your case would be:

onRegionClick: function (element, code, region) {
    if(code == 'us'){
        window.open("www.mywebsite.com", "_blank");
    }
}

Shorter version:

onRegionClick(element, code, region) => {
    if(code == 'us')
        window.open("www.mywebsite.com", "_blank");
}

For a dynamic URL, something like the following would work:

onRegionClick(element, code, region) => {
    if(code == 'us')
        window.open(`www.mywebsite.com/${code}`, "_blank");
}

Upvotes: 1

Ryan O'D
Ryan O'D

Reputation: 350

You could use the Event object .preventDefault() method on windown.open().

Upvotes: 1

Related Questions