I need to open this urls in a new tab. Right now they are opening in the same page.
var data = [
{title: "1", url: "gallery1.html"},
{title: "2", url: "gallery2.html"},
{title: "3", url: "gallery2.html"},
My knowledge in javascript is very short. I tried things like:
<script type='text/javascript'>
window.open(url);
</script>
or
.attr("xlink:href", function(d) { return d.example.url; })
.attr("xlink:target", "_blank")
.attr("xlink:title", function(d) { return d.example.title; });
Script47
14.6k4 gold badges49 silver badges69 bronze badges
2 Answers 2
window.open(url, "_blank");
This will open it in a new page.
_blank is a special keyword that will tell .open to open in a new page.
answered Aug 20, 2016 at 16:44
Derek 朕會功夫
94.8k45 gold badges199 silver badges255 bronze badges
Sign up to request clarification or add additional context in comments.
6 Comments
Derek 朕會功夫
@kajihood
data.forEach(link => open(link.url, "_blank")) This will open all links in your array.kajihood
where I have to put that?
Derek 朕會功夫
@kajihood Put it wherever you want to open a new page. If you have no programming experience I encourage you to do some readings on basic HTML5 apis.
kajihood
Can you please put an example with my code? I tried to copy your demo and it says syntax error.
Derek 朕會功夫
@kajihood Probably because I used ES6 syntax. Try converting it back to ES5 if you are using an older browser.
|
To loop trough your array do sth like this:
data.forEach(function(elem){
window.open(elem.url,"_blank");
});
answered Aug 20, 2016 at 16:46
Jonas Wilms
139k20 gold badges164 silver badges164 bronze badges
lang-js