javascript - links within an iframe window to open in new tab - Stack Overflow

I have seen this question many times, and a lot of the answers seem to suggest the base target="_b

I have seen this question many times, and a lot of the answers seem to suggest the base target="_blank" technique. However, I have used this before in the past; but my current page it does not work. I also don't think it could be best option even if it did work; as I ONLY want the links within the iframe src="" to open in a new window. I am hopping there's a simple solution I can add inline to the page. I have also tried adding an id as below, and using JavaScript, still nada.

<iframe src="mywordpressfeed.html" id="frame1" width="310" height="380"></iframe> 

JS

$(document).ready(function(){
    $("#frame1").attr("target","_blank");
});

Basically the goal is to when a user sees my wordpress feed within the iframe I have on a static page; once the post title is clicked it loads in a new window - as now it loads within the same iframe so there isn't an increased level of readability.

I have seen this question many times, and a lot of the answers seem to suggest the base target="_blank" technique. However, I have used this before in the past; but my current page it does not work. I also don't think it could be best option even if it did work; as I ONLY want the links within the iframe src="" to open in a new window. I am hopping there's a simple solution I can add inline to the page. I have also tried adding an id as below, and using JavaScript, still nada.

<iframe src="mywordpressfeed.html" id="frame1" width="310" height="380"></iframe> 

JS

$(document).ready(function(){
    $("#frame1").attr("target","_blank");
});

Basically the goal is to when a user sees my wordpress feed within the iframe I have on a static page; once the post title is clicked it loads in a new window - as now it loads within the same iframe so there isn't an increased level of readability.

Share Improve this question edited Jun 16, 2013 at 0:39 Lieutenant Dan asked Jun 15, 2013 at 23:31 Lieutenant DanLieutenant Dan 8,29828 gold badges97 silver badges216 bronze badges 3
  • 2 "target" is an attribute of the anchor tag, not the iframe tag. – Andbdrew Commented Jun 15, 2013 at 23:39
  • Can you not edit your Wordpress theme/code/feed to give hyperlinks the target="_blank" option? – adaam Commented Jun 15, 2013 at 23:41
  • Thanks for the response @adaam but no, because when users are there I don't want it to be a new page per post click. Only on the splash homepage where I have the iframe. – Lieutenant Dan Commented Jun 15, 2013 at 23:46
Add a ment  | 

2 Answers 2

Reset to default 2

There is no real solution to this, due to the iFrame tag being developed for the opposite.

//pass the iframe to this iframe getting function 
    function iframeRef( frameRef ) {
        return frameRef.contentWindow ? frameRef.contentWindow.document : frameRef.contentDocument
    }
//Get Iframe
    var inside = iframeRef( document.getElementById('iframeID') );
//Get all links
    var links = inside.getElementsByTagName('a');
//Loop throught links and set their attributes
    for (var i = 0 ; i<links.length ; i++){
    links[i].setAttribute('target','_blank');
    }
//No jQuery needed!

thanks to meder

EDIT Due to iframe same source restrictions I had to find a website with inner iframe from same source so you can paste this code

//pass the iframe to this iframe getting function 
    function iframeRef( frameRef ) {
        return frameRef.contentWindow ? frameRef.contentWindow.document : frameRef.contentDocument
    }
//Get Iframe
    var inside = iframeRef( document.getElementById('IFwinEdit_Gadget_247730_3349') );
//Get all links
    var links = inside.getElementsByTagName('input');
//Loop throught links and set their attributes
    for (var i = 0 ; i<links.length ; i++){
        links[i].setAttribute('style','background:red');
    }
//No jQuery needed!

to the console in this web site and see the inputs change color

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744728501a4590324.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信