简体   繁体   中英

Can't Open Page in New Tab Using Javascript

I can't figure out how to open a new tab using JavaScript. I went through related questions but nothing worked for me. I want the browser to open a new tab when someone clicks on the link (example.com) below. What code should I add and where should I place that code?

My html code is here -

<section id="work-grid" class="site-inner">
<div class="work-item" data-groups='["all", "webdesign"]' data-url="http://example.com">
<figure class="image-container"><img src="images/work/web-one.jpg" /></figure>
</div>

Thank you in advance.

Use the attribute newtab on the a element.

Code for driver for newtab a attribute:

(()=>{document.getElementsByTagName("a").map((e)=>{if(e.newtab==="newtab"){e.target="_blank";e.rel="noopener noreferrer";}return e;});})();

Beautified version without an anonymous function:

document.getElementsByTagName
(
  "a"
).map
(
  (e) =>
  {
    if (e.newtab === "newtab") {
      e.target = "_blank";
      e.rel = "noopener noreferrer";
    }
    return e;
  }
);

You can only put the compressed version on your page due to uncompressed-code checking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM