How can I call the parent-window jquery func开发者_StackOverflow中文版tion with variable from popup window javascript? Any simple examples that I can look at?
opener is a reference to the window
object of the opening document. I.e. you have access to the global javascript namespace of the opening window.
e.g.
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<script type="text/javascript">
function foo(url) {
// if there is a reference to the opening window
if (null!=opener) {
// we call the function in the context of the opening window
opener.foo(url);
}
else {
// otherwise show the data
$('#d1').html(new Date() + " : " + url);
}
}
</script>
</head>
<body>
<div id="d1">...</div>
<button onclick="window.open('?');">new window</button>
<button onclick="foo(document.URL);">propagte url</button>
</body>
</html>
if you press "propagate url" in a (in any) popup window the function call will bubble up to the first non-popup window (having opener=null).
edit: keep in mind that security restrictions (like cross domain checks) implemented in the browser apply.
edit2: example with history.go(0)
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<script type="text/javascript">
function foo() {
var context = (null!=opener) ? opener : window;
context.history.go(0);
}
$(document).ready( function() {
$('#d1').html("document ready at "+ new Date());
});
</script>
</head>
<body>
<div id="d1">...</div>
<button onclick="window.open('?');">new window</button>
<button onclick="foo(document.URL);">...and action</button>
</body>
</html>
Normally you define a plugin function like this:
(function($) { // jquery no conflict
$.fn.myPlugin = function(myvar){
// do something
}
})(jQuery);
Then use this:
window.opener.$.fn.myPlugin(myvar);
window.opener.jQuery(myVariable);
精彩评论