JS操作Iframe中属性、事件(跨域)



实现目标

跨域控制页中的属性,调用页面中按钮事件。

实现背景

背景一:打开百度,自动查询XXX关键字。

背景二:已知用户名、密码情况下,自动登录。

实现方法

方法一:C#代码实现,使用WebBrowser

方法二:脚本实现,使用ActiveXObject,动态创建页面

实现代码

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

   <title>JS操作Iframe中属性、事件(跨域)</title>

 

   <scripttype="text/javascript">

   //参考:http://www.woyaofeng.com/409.html

   function page_load()

   {

       //创建一个IE窗口

       var ie = newActiveXObject("InternetExplorer.Application");

       //显示

       ie.visible = true;  

 

       ie.navigate("http://www.baidu.com");

       

       //等待加载完毕

       while(ie.busy){

       }  

       

       //获得windowdocument和表单的引用

       var document = ie.document;

       var window = document.parentWindow;

       var form = document.forms[0];  

       

       //通过控件 ID 赋值

       window.document.getElementById("kw").value= "新闻热点";

       

       //通过控件 Name 赋值

       //form.wd.value = "新闻热点";

       

       //页面iframe中控件赋值

       //window.document.getElementById("indexFrame").contentWindow.document.getElementById("username").value= "admin";

       

       window.document.getElementById("su").click();

   }

   function page_close()

   {

       window.opener=null

       window.open("","_self")

       window.close();

   }

   </script>

 

</head>

<body onload="page_load();page_close();">

</body>

</html>

运行方式

html标签中内容拷贝至记事本,另存为html格式文件,使用浏览器浏览(IE可能提示安全性,需要允许)。

你可能感兴趣的:(js,跨越)