简体   繁体   English

JS:更改iframe链接代码内部

[英]JS: change inside iframe link tags

I'm trying to change the href attribute of all the a tags inside an iframe from outside of the iframe. 我正在尝试从iframe外部更改iframe中所有a标签的href属性。

Here is what I have so far: 这是我到目前为止的内容:

<Html>
<Head>
    <Title>change links</Title>
</Head>
<body>
    <iframe style="width:100%;height: 100%;border:0;" id="tab" src="http://www.anyside.co.il" ></iframe>
    <script type="text/javascript">
        var frame = document.getElementById("tab");   
        var tags = frame.getElementsByTagName("a");   
        for (var i = 0; i < tags.length; i++) {   
            tags[i].href = 'javascript:alert("")';  
        } 
</script>
</body>
</Html>

What is wrong in this code that it doesn't work? 这段代码有什么错,它不起作用?

firstly it have to be the same domain 首先,它必须是相同的域

secondly it should be : 其次应该是:

        var frame = window.frames["tab"].document;
    var tags = frame.getElementsByTagName("a");   
    for (var i = 0; i < tags.length; i++) {   
        tags[i].href = 'javascript:alert("")';  
    } 

or : 要么 :

var frame = document.getElementById("tab").contentDocument;   
    var tags = frame.getElementsByTagName("a");   
    for (var i = 0; i < tags.length; i++) {   
        tags[i].href = 'javascript:alert("")';  
    }

( in your code you only have the iframe tag, but you need the document of the frame of this tag ) (在您的代码中,您只有iframe标记,但是您需要此标记的框架文档)

and thirdly you should either execute the code when iframe is loaded ( see the event onload of the iframe ), or use a setTimeout with a timeout enough big for any connection speed. 第三,您应该在加载iframe时执行代码(请参阅iframe的事件onload),或者使用setTimeout,其超时对于任何连接速度而言都足够大。

Edit : 编辑:

In the comments it seems it's more about being on different domain, if it's the case you can't use javascript or frame to access or modify content accross domain. 在注释中,似乎更多的是位于不同的域上,如果是这种情况,则不能使用JavaScript或框架来访问或修改整个域中的内容。

If it's a static page, just host it yourself, if it's dynamic and it's okay getting a proxy copy of the page, you could use several way : 如果它是静态页面,请自己托管,如果它是动态页面,并且可以获取页面的代理副本,则可以使用以下几种方法:

  1. use a server side language ( like php ) on the same server to get and display the page, then use this page on the same server in the iframe ( then you will be able to have access to it ) 在同一台服务器上使用服务器端语言(例如php)来获取并显示该页面,然后在iframe中的同一台服务器上使用该页面(那么您将可以访问它)
  2. yql could be a way too it can get a web page with javascript yql可能也是一种可以使用javascript获取网页的方法

Well it's hard to say not knowing what you want to do, maybe the owner of the website could do something, or for example if you just want to add tips on the website for your users making them install an user script wich will execute on the website could be a better way, without knowing why you need this and what's the situation, it's hard to say. 好吧,很难说不知道您想做什么,也许网站的所有者可以做些什么,或者例如,如果您只想在网站上为您的用户添加提示,以使他们安装在网站上执行的用户脚本,网站可能是一个更好的方法,不知道为什么需要这样做以及情况如何,很难说。

由于安全限制,您的JS代码无法从iframe访问DOM元素(iframe元素位于不同的域中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM