以编程方式点击iframe中的JavaScript链接 [英] Programatically click link in iframe with Javascript

查看:62
本文介绍了以编程方式点击iframe中的JavaScript链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的html / javascript。下面有两条if语句,我已经尝试过了。我用indexof来获取标签之间的文本。还试过href ==使用href =内容。

 < html xmlns =http://www.w3.org/1999/xhtml>> ; 
< head>
< title> pb< / title>
< script type =text / javascript>
函数调用(){
alert(Called!);
var allAnc = document.getElementById(d)。contentDocument.getElementsByTagName(a);
for(var i = 0; i< allAnc.length; i ++){
if(allAnc [i] .href.indexOf(Flag)> 0){
alert (通过indexof找到链接,尝试点击!);
(allAnc [i])。click();
休息;

if(allAnc [i] .href ==javascript:flag.closeit()){
alert(通过href找到链接,尝试点击!
(allAnc [i])。click();
休息;
}
}
}
< / script>
< / head>
< body>
< input id =Button1type =buttononclick =call()value =button/>
< iframe id =dsrc =sourceurl.htmlwidth =100%height = 700>< / iframe>
< / body>
< ; / html>

以下是我试图点击的iframe中的链接:

 < a style =text-align:left;href =javascript:flag.closeit()title =Flag> ; Flag< / a> 


解决方案

正在将不同域的内容加载到您的iframe中,因此您无法阅读该域的内容,原因是相同的原产地政策限制。

Here's my html/javascript. Below there are two if statements that I've tried. I used indexof to get the text between the tags. Also tried href == to use the href= contents. Both do not work.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>pb</title>
    <script type="text/javascript">
    function call() {
        alert("Called!");
        var allAnc = document.getElementById("d").contentDocument.getElementsByTagName("a");
        for (var i = 0; i < allAnc.length; i++) {
            if (allAnc[i].href.indexOf("Flag") > 0) {
                alert("Found link by indexof, Trying to click!");
                (allAnc[i]).click();
                break;
            }
            if (allAnc[i].href == "javascript:flag.closeit()") {
                alert("Found link by href, Trying to click!");
                (allAnc[i]).click();
                break;
            }
        }
    }
</script>
</head>
<body>
    <input id="Button1" type="button" onclick="call()" value="button" />
    <iframe id="d" src="sourceurl.html" width="100%" height=700"></iframe>
</body>
</html>

Here is the link inside the iframe I am trying to click:

<a style="text-align: left;" href="javascript:flag.closeit()" title="Flag">Flag</a>

解决方案

From your comment, you are loading the content of different domain into you iframe, so you just can't read the content of that domain due to same origin policy restrictions.

这篇关于以编程方式点击iframe中的JavaScript链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆