Trying different options like I have page opened in new window below opens in newtab
<a href="imageurl" target="_blank">
<img src="imageurl" style="width: 50%; height: 220px;">
</a>
Below overrides the current window
<img src="imageurl" style="width: 50%; height: 220px;" onclick="window.open(this.src)">
Looking to open in new window again. Please suggest
Trying different options like I have page opened in new window below opens in newtab
<a href="imageurl" target="_blank">
<img src="imageurl" style="width: 50%; height: 220px;">
</a>
Below overrides the current window
<img src="imageurl" style="width: 50%; height: 220px;" onclick="window.open(this.src)">
Looking to open in new window again. Please suggest
Share Improve this question edited Nov 6, 2020 at 16:25 Swati 28.5k4 gold badges25 silver badges44 bronze badges asked Nov 6, 2020 at 6:14 Nagappa L MNagappa L M 1,4805 gold badges20 silver badges34 bronze badges2 Answers
Reset to default 8To trigger a new window instead of a new tab (something you should generally be reluctant to do) you need to specify some window features which the browser won't apply to a tab.
Setting resizable
is sufficient in Chrome. I haven't tested in other browsers.
HTML:
<a href="http://placekitten./100/100" target="_blank">
<img src="http://placekitten./100/100">
</a>
JS:
const openInNewWindow = event => {
event.preventDefault();
const {href, target} = event.currentTarget;
const features = "resizable";
window.open(href, target, features);
};
document.querySelector("a")
.addEventListener("click", openInNewWindow);
(Don't bind click event listeners to images, they aren't designed as interactive controls. Users who depend on (for example) a screen reader or a keyboard focus to interact with a document will find it difficult or impossible to trigger a click on an image. Use semantic markup. If you are linking something, use a link, then enhance with JS.)
Try like
window.open(this.src,"_blank");