


Methods and precautions for calling JS in the child parent class window in iframe_javascript skills
1. Foreword
My page uses EasyUI’s pop-up window with an iframe embedded in it.
First: When the parent window opens the child window, it is an iframe subpage that adds user information. After clicking Save, the child window iframe calls the function closeAddWindow() method of the parent window, allowing the parent window to close the newly added page;
Second: The parent window opens an iframe sub-window to set user permissions. First, opening this sub-window will load all existing permissions in the database table, and then the sub-window needs to splice the loaded permission information into html Append to an ID For
Okay, that’s it, let’s take a look at more examples! ! ~~~~~~(*^__^*) Hee hee...
2. iframe child and parent window method calls
2.1 Grammar usage
1. Parent window embedded iframe
2. The parent window calls the child window method
myFrame.window.sonMethod();
3. The child window calls the parent window method
parent.fatherMethod();
4. Browser-compatible iframe loading completion method
if (myFrame.attachEvent) { myFrame.attachEvent("onload", function () { alert("兼容IE加载的加载方法"); }); } else { myFrame.onload = function () { alert("兼容其他浏览器加载方法"); }; }
2.2 Grammar code
Father.html
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> </head> <body> <div>我是父窗口内容</div> <div><input type="button" id="btnFather" value="调用子窗口方法" /></div> <br /> <br /> <br /> <iframe id='myFrame' name="myFrame" src="FChild.html" width='100%' height='100%' frameborder='0'></iframe> <script type="text/javascript"> document.getElementById("btnFather").onclick=function () { myFrame.window.sonMethod(); } function fatherMethod() { alert("父窗口方法!"); } if (myFrame.attachEvent) { myFrame.attachEvent("onload", function () { alert("兼容IE加载的加载方法"); }); } else { myFrame.onload = function () { alert("兼容其他浏览器加载方法"); }; } </script> </body> </html>
FChild.html
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <div style="border:1px solid red;"> 我是子窗体内容</div> <div > <input type="button" id="btnSon" value="调用父窗口方法" /></div> <script type="text/javascript"> document.getElementById("btnSon").onclick = function () { parent.fatherMethod(); } function sonMethod() { alert("子窗口方法!"); } </script> </body> </html>
3. When to use myFrame.onload or myFrame.attachEvent
The easyui framework front-end framework is used here
<div id="divRoleUsers" title="设置用户角色" class="easyui-window" closed="true" collapsible="false" minimizable="false" maximizable="false" style="width: 140px; height: 250px; padding: 5px;"> </div> <div > <input type="button" id="btn" value="设置用户角色" /></div> <script type="text/javascript"> $("#btn").click(function () { showSetUserRoleWindow(); }); //设置用户角色 function showSetUserRoleWindow() { var getSelections = $("#tt").datagrid("getSelections"); if (getSelections.length > 1 || getSelections.length == 0) { $.messager.alert("错误提示", "请选中一行数据!", "error"); return false; } var data = getSelections[0]; //获取选中的一行所有json的数据 //if ($("#divRoleUsers #iframe").length != 0) { // $("#divRoleUsers #iframe").remove(); //} // $('#divRoleUsers').append("<iframe id='iframe' name='iframe' src='RoleUsers_Update.aspx?UserID=" + data.UserID + "' width='100%' height='100%' frameborder='0'></iframe>"); //错误做法!:上面src='RoleUsers_Update.aspx?UserID=" + data.UserID + "' 这里通过拼接参数iframe的src, //然后通过子窗口 parent.document.getElementById("iframe").getAttribute("src");//获取父窗体iframe的src 发现根据获取不到UserID的值,为null,也是因为加载顺序先后的问题,导致我要用给iframe注册onload事件后才能获取到我需要的结果 //if (myframe.attachEvent) { // myframe.attachEvent("onload", function () { // alert("Local iframe is now loaded."); // myframe.window.loadAllRole(); // }); //} else { // myframe.onload = function () { // alert("Local iframe is now loaded."); // myframe.window.loadAllRole(); // }; //} if ($("#divRoleUsers #myframe").length != 0) { //这一步是必须的!!!,因为不加这一句下面onload绑定事件只执行一次,我需要每次加载完iframe都调用一次子窗口的方法! $("#divRoleUsers #myframe").remove(); } $('#divRoleUsers').append("<iframe id='myframe' name='myframe' src='RoleUsers_Update.aspx' width='100%' height='100%' frameborder='0'></iframe>"); if (myframe.attachEvent) { myframe.attachEvent("onload", function () { myframe.window.loadAllRole(); myframe.window.loadUserRole(data.UserID); }); } else { myframe.onload = function () { myframe.window.loadAllRole(); //调用子窗口iframe里面的方法加载所有的角色checkbox myframe.window.loadUserRole(data.UserID); //接着传递用户ID过去给子窗口的方法,给用户拥有的角色设置checkbox选中 }; } $('#divRoleUsers').window('open'); } </script>
4. Let’s summarize a few key points
When calling the parent-child window method, pay attention to the order of loading to get the desired results. If you encounter problems, you can often use alter() to test or monitor the browser's developer tools
The above content is the method and precautions for calling JS in the sub-parent class window of iframe in this article. I hope you like it.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

JavaScript can be run in PowerPoint, and can be implemented by calling external JavaScript files or embedding HTML files through VBA. 1. To use VBA to call JavaScript files, you need to enable macros and have VBA programming knowledge. 2. Embed HTML files containing JavaScript, which are simple and easy to use but are subject to security restrictions. Advantages include extended functions and flexibility, while disadvantages involve security, compatibility and complexity. In practice, attention should be paid to security, compatibility, performance and user experience.
