最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get all links inside iframe and add blank target attribute - Stack Overflow

programmeradmin3浏览0评论

Is it possible to retrieve all href elements on a page that is an iframe?

Meaning, I have a page that is iframing a page from different site. What I am showing in the iframe contains a number of links (href tags).

Is it possible to add a TARGET attribute of BLANK to all links inside the iframe? The links are buried inside a number of divs and tables, so I need to be able to recursively add the attribute inside many nested tables/divs.

EDIT: Added screenshot to show extra characters needed to be removed:

Is it possible to retrieve all href elements on a page that is an iframe?

Meaning, I have a page that is iframing a page from different site. What I am showing in the iframe contains a number of links (href tags).

Is it possible to add a TARGET attribute of BLANK to all links inside the iframe? The links are buried inside a number of divs and tables, so I need to be able to recursively add the attribute inside many nested tables/divs.

EDIT: Added screenshot to show extra characters needed to be removed:

Share Improve this question edited Jan 19, 2012 at 5:48 obautista asked Jan 17, 2012 at 6:49 obautistaobautista 3,77315 gold badges55 silver badges87 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

If the iframe src it's on the same domain you can easily access it by using document.getElementById("frameID").document and manipulate that.

If it's a different domain you may consider link the iframe to a script in your own domain, and make that script download the data from the remote domain and print out:)

myHTMLpage.html

<html>
    <head>
    <title></title>
        <script src="jquery-1.7.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#ifr").load(function () {
                var ifr = document.getElementById("ifr")
                var anchors = ifr.contentDocument.getElementsByTagName("a");
                for (var i in anchors) {
                    anchors[i].setAttribute("target", "_blank");
                }
            });
        });
    </script>
    </head>
    <body>
        <iframe src="myOwnSite.aspx" width="900px" height="600px" id="ifr" ></iframe>
    </body>
</html>

myOwnSite.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyOwnSite.aspx.cs" Inherits="MyOwnSite" %>

myOwnSite.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;


public partial class MyOwnSite : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Response.Write(new WebClient().DownloadString("http://theExternalSiteHere."));
        }
    }
}

Try adding <base target="_blank"> inside the head tag of your iframe.

No, this is not possible if the site inside the iframe is not on your domain which according to your description seems to be the case.

This is what worked for me:

var anchors= document.getElementById(id).contentWindow.document.getElementsByTagName("a"); for (var i in anchors) { anchors[i].target = "_blank"; //.setAttribute("target", "_blank"); }

I have IE8. The script for IE 6 and IE 7 can be different.

发布评论

评论列表(0)

  1. 暂无评论