0

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">
  • Your task is impossible because of Same Origin Policy – epascarello Sep 01 '22 at 12:40
  • Then how Google AdX/Google Ad manager have a option to open ads in new Tabs? – user19895714 Sep 01 '22 at 12:41
  • 2
    because google is altering their ad code, they are not trying to inject a script into the iframe. – epascarello Sep 01 '22 at 12:44
  • If your able to intercept route changes you could prevent from following it and create a new tab with the ad link. Don't know how your handling routes, but I think it could be doable. Something like navigation guard. – Leonardo Bezerra Sep 01 '22 at 12:45
  • @epascarello Google Adx even make Ads open in new tab from third-party. I am not trying to manipulate code in Iframe but i am trying to force my webpage to open all the links in new tab. – user19895714 Sep 01 '22 at 12:49
  • 1
    What we are saying is there is no way to run JavaScript on the content in the iframe. Only control you have over iframes is the sandbox attribute https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#attr-sandbox – epascarello Sep 01 '22 at 12:57
  • Your ads are probably _not_ inside iframes, usually the scripts given to you by those ad service providers, dynamically insert elements directly into your page. But this will most likely happen in an asynchronous fashion - so your code trying to manipulate link targets, would need to run _after_ this has happened. And since there likely will be no event for that, you could only run this code in an interval then. (And even that will only work, if the ads don't use shadow DOM by now to "encapsulate" themselves from the rest of your page.) – CBroe Sep 01 '22 at 13:00

1 Answers1

1

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Checking the documents. i am still rookie. can you help me how i can use this with my code or my need? – user19895714 Sep 01 '22 at 12:41
  • 1
    No. The answer says what you want can't be done. – Quentin Sep 01 '22 at 12:42
  • will this `` work? – user19895714 Sep 01 '22 at 12:55
  • 2
    Only for links in your main document, not for anything that is contained in an iframe - for that, the document that is loaded into the iframe itself, would need to contain this base element. – CBroe Sep 01 '22 at 12:58
  • 1
    (And it would only work on links that do not explicitly specify a target themselves. If the ad code inserted a link with `target="_self"` or any other specific target, then a base element target would of course _not_ overwrite this.) – CBroe Sep 01 '22 at 13:03