JS操作Iframe中属性、事件

实现效果

1.打开百度,自动查询XXX关键字
2.已知用户名、密码情况下,自动登录。

实现方式

1.C#代码实现,使用WebBrowser
2.脚本实现,使用ActiveXObject,动态创建页面

代码示例

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>JS操作Iframe中属性、事件(跨域)</title>

    <script type="text/javascript">
    //参考:http://www.woyaofeng.com/409.html
    function page_load()
    {
        //创建一个IE窗口 
        var ie = new ActiveXObject("InternetExplorer.Application"); 
        //显示 
        ie.visible = true;   

        ie.navigate("http://www.baidu.com");
        
        //等待加载完毕 
        while(ie.busy){
        }   
        
        //获得window和document和表单的引用 
        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>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,798评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,117评论 6 342
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 99,973评论 9 468
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,892评论 25 709
  • 昏黯地灯光 轻柔地音乐 浪漫地装饰 空气中弥漫着烟味酒味 深情相视 羞涩地傻笑 随意地瞎聊 带着迷人地微笑 时间变...
    田小姣阅读 2,990评论 6 2