[英]Get all links inside iframe and add blank target attribute
Is it possible to retrieve all href elements on a page that is an iframe? 是否可以检索作为iframe的页面上的所有href元素?
Meaning, I have a page that is iframing a page from different site. 意思是,我有一个页面来自不同的网站iframing页面。 What I am showing in the iframe contains a number of links (href tags).
我在iframe中显示的内容包含许多链接(href标记)。
Is it possible to add a TARGET attribute of BLANK to all links inside the iframe? 是否可以将BLANK的TARGET属性添加到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.
链接隐藏在许多div和表中,因此我需要能够在许多嵌套表/ div中递归地添加属性。
EDIT: Added screenshot to show extra characters needed to be removed: 编辑:添加了截图以显示需要删除的额外字符:
If the iframe src it's on the same domain you can easily access it by using document.getElementById("frameID").document and manipulate that. 如果iframe src位于同一个域中,您可以使用document.getElementById(“frameID”)轻松访问它。记录并操作它。
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:) 如果它是一个不同的域,您可以考虑将iframe链接到您自己域中的脚本,并使该脚本从远程域下载数据并打印出来:)
myHTMLpage.html 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 myOwnSite.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyOwnSite.aspx.cs" Inherits="MyOwnSite" %>
myOwnSite.aspx.cs 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.com"));
}
}
}
不,如果iframe中的网站不在您的域中,根据您的描述似乎是这种情况,这是不可能的。
尝试在iframe的head标记内添加<base target="_blank">
。
This is what worked for me: 这对我有用:
var anchors= document.getElementById(id).contentWindow.document.getElementsByTagName("a"); var anchors = document.getElementById(id).contentWindow.document.getElementsByTagName(“a”); for (var i in anchors) { anchors[i].target = "_blank";
for(var i in anchors){anchors [i] .target =“_ blank”; //.setAttribute("target", "_blank");
//.setAttribute(“target”,“_ blank”); }
}
I have IE8. 我有IE8。 The script for IE 6 and IE 7 can be different.
IE 6和IE 7的脚本可以不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.