1

I have the following code in an HTML file along with the jquery file. When I run this HTML file and then test it to see if every clickable link is forced to be opened in the same tab, it doesn't make any difference and continues to open links in new tabs. I'm not especially opening anything in a new tab by holding down Ctrl.

I'm trying it on links by default and a simply mouse click still opens it in a new tab.

 <html>
    <head>
        <style type="text/css">
        body {width:780; height:580;}
        </style>
    <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js">
    </script>
    <script>
    $(document).ready(function(){
    $('a[target="_blank"]').removeAttr('target');
        });
    </script>
    </head>

    <body>
        <iframe src= "https://www.bing.com/" width="100%" height="100%" frameborder="0" id="MyFrame">
        </iframe>
    </body>
    </html>
Ansh
  • 175
  • 1
  • 2
  • 13
  • 2
  • 1
    Your document itself doesn’t contain any links. And what’s inside the iframe, which has its content loaded from a different domain, is of course off limits to the scripts running in the context of _your_ page, because **Same Origin Policy**. – CBroe May 15 '18 at 12:05
  • @CBroe I see, so simply speaking, unless the command comes from the website's end to remove target="_blank", it will not work since my command to it doesn't have any authority? – Ansh May 15 '18 at 12:06
  • Yes. (Go read up on the bold keyword, if it means nothing to you yet.) This would either need to be done by a script running directly inside the document that was loaded into the iframe; or from the context of a browser extension. – CBroe May 15 '18 at 12:08
  • Are you sure it's not just your browser that is configured that way? Some browsers have the option to open any link in a new tab – Glubus May 15 '18 at 12:11
  • @CBroe Hey, you know I was actually trying this for a page that would open in the popup of my extension itself. Hence the iframe. In that case, I added Javascript too which don't seem to make any difference so I suppose it's all about the Same Origin Policy. – Ansh May 15 '18 at 12:14
  • 1
    Just because you somehow “invoke” your document via a browser extension, doesn’t invalidate the SOP. This is still a document loaded from origin X, that embeds another document from origin Y. Your browser extension would have to interact with the content loaded from the other domain directly, via the mechanisms provided for that in such a context. – CBroe May 15 '18 at 12:18
  • No it is not about the same origin policy.... the content in the iframe is NOT part of the current page so you can NOT select it. You would have to select the iframe and than its contents to find it. `$("iframe").contents().find("a")` is what you would need to do. BUT since the iframe is in a DIFFERENT domain, it is not possible to select the elements, that is where the same origin policy comes into effect. – epascarello May 15 '18 at 12:22

1 Answers1

-1

$('a[target="_blank"]').removeAttr('target');

don't use _blank there..

Sandesh Gupta
  • 1,175
  • 2
  • 11
  • 19