Ning Kailiang's Website Building Blog 简体中文
jQuery pass values from child page to parent page

jQuery pass values from child page to parent page:How can a child page pass values to a parent page using jQuery in 2026?

Author:Ning Kailiang's Website Building Blog · Date:20260929 · Cooperation · Report

This page answers the following questions about“jQuery pass values from child page to parent page”:How can a child page pass values to a parent page using jQuery in 2026?What is the recommended jQuery method to send data from a child iframe to its parent page in 2026?Can jQuery be used with window.opener to pass values and trigger parent callbacks in 2026?

Q: How can a child page pass values to a parent page using jQuery in 2026?

A: In 2026, the classic html">approach is still using window.opener, but with modern safety checks. If the child window was opened via window.open(), the parent is accessible through window.opener. From the child page you can call a function defined on the parent, for example: window.opener.setValue('data'). The parent page defines window.setValue = function(value){ $('#result').text(value); }. To avoid cross-origin issues, both pages must share the same origin, and in 2026 browsers still enforce this strictly. Also, window.opener may be null if you used rel='noopener' or a noopener feature. For better compatibility, use postMessage instead, which works across origins and iframes. The child can do window.opener.postMessage({key:'value'}, 'https://parent-origin.com'), and the parent listens with window.addEventListener('message', function(e){ if(e.origin !== 'https://child-origin.com') return; console.log(e.data); }). Always validate the origin for security. With jQuery, you can still use $.parseJSON for older data, but native JSON is standard now.

Q: What is the recommended jQuery method to send data from a child iframe to its parent page in 2026?

A: For same-origin iframes in 2026, the simplest jQuery-friendly method is the postMessage API combined with jQuery event handling. Inside the child iframe, use parent.postMessage({user:'Alice', score:42}, 'https://parent-domain.com'). On the parent page, listen for the message event with jQuery: $(window).on('message', function(event){ if(event.originalEvent.origin !== 'https://child-domain.com') return; var data = event.originalEvent.data; $('#output').text(data.user + ': ' + data.score); }). This html">approach is secure because you check event.originalEvent.origin. If you need to support very old browsers (pre-IE10), jQuery alone cannot bridge cross-origin iframes securely, so avoid direct parent.document access. For same-origin, you could also use window.parent.$('#element').val('test'), but that tightly couples the pages. In 2026, best practice is to decouple with postMessage and use jQuery only for DOM updates on the parent. This keeps the child page independent and avoids breaking changes when the parent's DOM structure changes.

Q: Can jQuery be used with window.opener to pass values and trigger parent callbacks in 2026?

A: Yes, jQuery can still be used with window.opener in 2026 for same-origin popups, but you must handle modern restrictions carefully. After the child page loads, you can access the parent's jQuery object via window.opener.$ if the parent has jQuery loaded. For example, in the child: var parentJQ = window.opener.$; parentJQ('#message').text('Hello from child');. You can also call a custom parent function: window.opener.handleChildData({id: 123}); where the parent defines handleChildData as a global function using jQuery internally. However, if the child was opened with rel='noopener' or target='_blank' with noopener, window.opener becomes null for security. Also, cross-origin access will throw a SecurityError. To avoid issues, always test for window.opener and wrap in try/catch: try { if(window.opener && !window.opener.closed) { window.opener.$('#status').val('done'); } } catch(e) { console.warn('Cross-origin or blocked'); }. For new projects in 2026, postMessage is preferred, but window.opener with jQuery remains valid for legacy same-origin popup flows.

jQuery pass values from child page to parent page

Dialogue about

Common scenarios of "jQuery pass values from child page to parent page"

【Learner】 Hi, I'm working on a project with a parent page and a child page (popup). I need to pass some data from the child page back to the parent page using jQuery. Can you help me understand how to do that?

【Expert】 Sure! There are several ways to pass data from a child page to a parent page using jQuery. The most common approach is using the `window.opener` object if the child page is opened via `window.open()`. Alternatively, you can use `postMessage` for cross-origin communication. Which method are you considering?

【Learner】 I think `window.opener` is what I need since the child page is opened by the parent using `window.open()`. Can you show me a simple example?

【Expert】 Absolutely. In the child page, you can access the parent window using `window.opener`. For example, if you want to set a value in an input field on the parent page, you can do: `$(window.opener.document).find('#parentInput').val('some value');`. Then you can close the child window if needed.

【Learner】 That seems straightforward. But what if I need to pass multiple values or an object? Should I use a function defined in the parent page?

【Expert】 Yes, calling a parent function is a clean way. In the parent page, define a function like `function receiveData(data) { ... }`. Then in the child page, you can call `window.opener.receiveData({ name: 'John', age: 30 });`. This way you can pass complex data.

【Learner】 What about security concerns? Is it safe to use `window.opener`?

【Expert】 Using `window.opener` is generally safe if both pages are from the same origin. However, if the child page is from a different domain, it could be a security risk. In that case, you should use `postMessage` with proper origin checks. Also, modern browsers may restrict `window.opener` for cross-origin popups.

【Learner】 I see. So if I control both pages and they are on the same domain, `window.opener` is fine. Now, what if the parent page needs to trigger something after receiving the data?

【Expert】 You can have the parent function perform any action, like updating the UI or making an AJAX call. For example, `receiveData` could update a table or show a message. Just make sure the function is defined before the child tries to call it.

【Learner】 What if the child page is opened as a modal dialog using a library like jQuery UI Dialog? That's not a separate window, so `window.opener` won't work.

【Expert】 Correct. If it's a modal within the same page, you're not dealing with a separate window. Instead, you can directly access parent page elements or call functions because it's all in the same document. You can use jQuery to find elements in the parent and set values, or use custom events.

【Learner】 So for same-page modals, I can just use `$('#parentElement').val('data')` from within the modal's JavaScript? But the modal content might be loaded via AJAX into a div.

【Expert】 Yes, if the modal content is loaded into the same DOM, you can still access parent elements directly. However, if the modal content is in an iframe, then you have a similar situation as separate windows. In that case, you can use `window.parent` from within the iframe to access the parent window.

【Learner】 Ah, iframe! That's another scenario. So if the child page is in an iframe, I can use `window.parent` to call parent functions?

【Expert】 Exactly. From the iframe, `window.parent` refers to the parent window. You can call `window.parent.receiveData(data)` just like with `window.opener`. Again, same-origin policy applies.

【Learner】 What if I need to pass data from parent to child? Is that similar?

【Expert】 Yes, you can access the child window from the parent using the reference returned by `window.open()`, or for iframes, `iframe.contentWindow`. For example, `var child = window.open(...); child.someFunction(data);`.

【Learner】 Great. Now, what about using `postMessage`? When would that be necessary?

【Expert】 `postMessage` is essential for cross-origin communication. It allows you to safely send messages between windows or iframes from different origins. You send a message with `otherWindow.postMessage(message, targetOrigin)`, and listen with `window.addEventListener('message', handler)`. It's more secure and flexible.

【Learner】 Thanks! I think I have a good understanding now. I'll use `window.opener` for same-origin popups, direct DOM manipulation for same-page modals, `window.parent` for iframes, and `postMessage` for cross-origin cases. Appreciate your help!

This article was published byNing Kailiang's Website Building Blog, For more knowledge about“Page” please followNing Kailiang's Website Building Blog。

Recent Articles