'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.
Solution 1:[1]
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");
}
Solution 2:[2]
You could use the Event object .preventDefault() method on windown.open().
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Ryan O'D |
