user19895714
user19895714

Reputation: 17

Open all links in New tab

We use _blank for a href URL. My website have some iframe, ads from Adsense, Taboola, etc. When user click on my iframe or ad it will getting opened in same window.

Is there anyway to force all the URL (Including Iframe, Ads, etc) to open in new Tab. so whenever someone click on link or ads, it will get opened in new window.

i am looking for a code which i can add in head or potion of div and it force to open i new tab.

I am trying

let links = document.links;

for (let i = 0; i < links.length; i++) {
    if (links[i].hostname != window.location.hostname) {
        links[i].target = '_blank';
    }
}

or

var myWin = window.open(strUrl, strWindowName);
var myWin = window.open(strUrl, strWindowName, [strWindowFeatures]);

or

   <base target="_blank">

Upvotes: 0

Views: 266

Answers (1)

Quentin
Quentin

Reputation: 943142

No.

Your existing code will loop through all the links in the current document.

To handle links in frames, you'll need to access the documents in those frames, but you can't do that cross-origin (which your adverts will be) without the co-operation of the owner.

Upvotes: 1

Related Questions