0

I have converted an excel file to an .htm file. In the excel file are links. I have embedded the .htm file in my website These links should be open in a new tab and not in the iframe.

How can I do this? target="_blank" didn't work.

<iframe src="website.htm" style="border:none;" width="100%" height="300">
</iframe>

This also not worked:

<script src="https://code.jquery.com/jquery-latest.js"></script>
<iframe src="website.htm" style="border:none;" width="100%" height="300">
</iframe>
<script>
$('iframe a').attr('target', '_blank');
</script>
coder007
  • 39
  • 1
  • 5
  • `iframeElement.contentWindow.document` is JavaScript from your page containing the iframe to access the iframe document. Program as usual. – StackSlave Mar 04 '19 at 09:03
  • @PHPglue what do you want to say?What should I do different? – coder007 Mar 04 '19 at 09:06
  • `$('iframe').eq(0).contents()` will give you the iframe document, with jQuery. I don't think iframe links will open in anything except the iframe window, so you will have to do that from the parent window, programmatically, if you want to use your iframe design. – StackSlave Mar 04 '19 at 09:16
  • Can you describe, how I can do it? – coder007 Mar 04 '19 at 09:43
  • The same way you do jQuery on the parent page. You have to as least understand jQuery to use it. I personally recommend that you separate data from the DOM *(Document Object Model)*. In other words you should just have a program on the Server that parses your excel sheet, and outputs that to JSON, for use in your HTML. Use AJAX to get data off the Server. Rarely use iframes. – StackSlave Mar 04 '19 at 09:49
  • The iframe contents will be protected by cross-domain restrictions. This may be very frustrating because you generally cannot reach into the iframe from outside, but it is an important security feature. You said you converted the Excel file to html yourself, so perhaps you have the html code available on your server? If so, you could load it with one of these techniques: https://stackoverflow.com/questions/8988855/include-another-html-file-in-a-html-file. If not, you probably need to fetch it as with an XMLHttpRequest and either CORS or (god forbid?) web-scraping. – Cat Mar 04 '19 at 10:34

2 Answers2

0

You could try

<iframe src="website.htm" style="border:none;" target="_top" width="100%" height="300">
</iframe>

Using jQuery:

$('iframe a').attr('target', '_blank');
G2 Jakhmola
  • 796
  • 1
  • 5
  • 15
0

Try it like below:

var linkList = [iframeElement].contentWindow.document.getElementsByTagName("a");
for (var i = 0; i < linkList.length; i++) {
    linkList[i].target = "_blank";
}
tinwan
  • 307
  • 1
  • 4
  • The iframe's src is not the same domain with the parent window? It's a question about cross domain.So you must modify the links in the iframe page and add "target='_blank'" to the links – tinwan Mar 04 '19 at 09:42
  • No, the iframe src is not the same as the parent window – coder007 Mar 04 '19 at 09:45
  • Ok, you can edit the iframe's inner page, and there's no any other way because the ifame acrosses origion from current page (Sorry, I'm not good at english), you can also search the answers via keywords : "iframe cross domain" – tinwan Mar 04 '19 at 09:54