jQuery Sharing between Parents and iFrames (inherit.js)
This plugin facilitates jQuery sharing between a parent page and its iFrames. Crucially, the iFrame content must originate from the same domain; otherwise, the plugin will malfunction. Download Link
/*! * jQuery iFrame Inheritance * * Copyright (c) 2009 Eric Garside (http://eric.garside.name) * Dual licensed under: * MIT: http://www.opensource.org/licenses/mit-license.php * GPLv3: http://www.opensource.org/licenses/gpl-3.0.html */ (function($) { // Global function for iFrame access via `parent.inherit()` this.inherit = function(child) { // Inject jQuery into the iFrame's DOM child.jQueryInherit = this.parent.jQuery; // Custom ready callback for iFrame scope child.jQueryInherit.fn.ready = function(fn) { child.jQueryInherit.hooks.bindReady(); if (child.jQueryInherit.hooks.isReady) fn.call(child.document, child.jQueryInherit); else child.jQueryInherit.hooks.readyList.push(fn); return this; }; // Namespace for iFrame hooks (document.ready, etc.) child.jQueryInherit.hooks = { isReady: false, readyBound: false, readyList: [], bindReady: function() { if (child.jQueryInherit.hooks.readyBound) return; child.jQueryInherit.hooks.readyBound = true; // DOMContentLoaded support (Mozilla, Opera, WebKit) if (child.document.addEventListener) { child.document.addEventListener("DOMContentLoaded", function() { child.document.removeEventListener("DOMContentLoaded", arguments.callee, false); child.jQueryInherit.hooks.ready(); }, false); } else if (child.document.attachEvent) { // IE support child.document.attachEvent("onreadystatechange", function() { if (child.document.readyState === "complete") { child.document.detachEvent("onreadystatechange", arguments.callee); child.jQueryInherit.hooks.ready(); } }); if (child.document.documentElement.doScroll && child == child.top) (function() { if (child.jQueryInherit.hooks.isReady) return; try { child.document.documentElement.doScroll("left"); } catch (error) { setTimeout(arguments.callee, 0); return; } child.jQueryInherit.hooks.ready(); })(); } jQuery.event.add(child, "load", child.jQueryInherit.hooks.ready); }, ready: function() { if (!child.jQueryInherit.hooks.isReady) { child.jQueryInherit.hooks.isReady = true; if (child.jQueryInherit.hooks.readyList) { jQuery.each(child.jQueryInherit.hooks.readyList, function() { this.call(child.document, child.jQueryInherit); }); child.jQueryInherit.hooks.readyList = null; } jQuery(child.document).triggerHandler('ready'); } } }; // Return a customized jQuery object for the iFrame return child.jQuery = child.$ = function(selector, context) { if (selector.constructor.toString().match(/Function/) != null) return child.jQueryInherit.fn.ready(selector); else return child.jQueryInherit.fn.init(selector || this.document, context || this.document); }; }; })(jQuery); /******* Inside the Child Element ******* * Call `parent.inherit(window)` in the iFrame's head to initialize jQuery. */ parent.inherit(window); // Example: document.ready in the iFrame parent.inherit(window)(function() { alert(jQuery('.someElementInTheiFrameDom').text()); });
Frequently Asked Questions (FAQs): jQuery, iFrames, and Inheritance
This section addresses common questions regarding jQuery usage within iFrames and cross-frame communication.
-
iFrame JavaScript Inheritance: iFrames do not inherit JavaScript from their parent. This plugin provides a workaround for jQuery specifically.
-
Data Sharing (Parent/iFrame):
postMessage
is the recommended method for secure cross-domain communication. Direct access viacontentWindow
is only possible for same-domain iFrames. -
iFrame CSS Inheritance: iFrames do not inherit CSS from their parent. Styles must be included within the iFrame's HTML or applied dynamically via JavaScript.
-
contentWindow
Property: This property references the iFrame's window object, enabling JavaScript interaction (same-domain only). -
postMessage
Method: This allows secure cross-origin messaging between windows. -
Same-Origin Policy: A crucial security mechanism restricting cross-domain access to an iFrame's content.
-
jQuery in iFrames: jQuery must be included within the iFrame's HTML.
-
Accessing iFrame Content (jQuery): The
.contents()
method allows access (same-domain only). -
AJAX in iFrames: AJAX can load content, but requires creating a new document within the iFrame and inserting the data.
-
Dynamic iFrame Creation (JavaScript):
document.createElement('iframe')
is used to create iFrames dynamically.
This revised response maintains the original information while improving clarity, structure, and readability. The code is also formatted for better visual presentation.
The above is the detailed content of jQuery Sharing between Parents and iFrames (inherit.js). For more information, please follow other related articles on the PHP Chinese website!

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











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.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript does not require installation because it is already built into modern browsers. You just need a text editor and a browser to get started. 1) In the browser environment, run it by embedding the HTML file through tags. 2) In the Node.js environment, after downloading and installing Node.js, run the JavaScript file through the command line.
