admin管理员组文章数量:1331449
I have a page that does not load jQuery. An <iframe>
included on that page (in the same domain) does load jQuery.
Using jQuery, is it possible to bind a function to an event of the window
object of the parent page, even though it doesn't load jQuery?
Accessing the parent window
doesn't seem to be a problem; this works (at least in Firefox 4):
console.log($(parent)); // displays "jQuery(Window index.html)" in the console
To give some context, I'm developing the document to be loaded within the <iframe>
, but I don't control the parent page, so I can't use a solution that requires adding jQuery to it. Ultimately I want to subscribe to the onscroll
event of the parent window
to execute a function in the <iframe>
every time that scrolling happens in the viewport.
I've tried (within the <iframe>
):
function my_function() {
console.log('Scrolling...');
}
$(parent).scroll(my_function);
However, that didn't work.
Any help would be appreciated.
Here is a working example:
.html
The document in the <iframe>
is here:
.html
I have a page that does not load jQuery. An <iframe>
included on that page (in the same domain) does load jQuery.
Using jQuery, is it possible to bind a function to an event of the window
object of the parent page, even though it doesn't load jQuery?
Accessing the parent window
doesn't seem to be a problem; this works (at least in Firefox 4):
console.log($(parent)); // displays "jQuery(Window index.html)" in the console
To give some context, I'm developing the document to be loaded within the <iframe>
, but I don't control the parent page, so I can't use a solution that requires adding jQuery to it. Ultimately I want to subscribe to the onscroll
event of the parent window
to execute a function in the <iframe>
every time that scrolling happens in the viewport.
I've tried (within the <iframe>
):
function my_function() {
console.log('Scrolling...');
}
$(parent).scroll(my_function);
However, that didn't work.
Any help would be appreciated.
Here is a working example:
http://troy.onespot./static/stack_overflow/onscroll/parent.html
The document in the <iframe>
is here:
http://troy.onespot./static/stack_overflow/onscroll/iframe.html
Share Improve this question edited Mar 19, 2022 at 17:46 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Mar 30, 2011 at 19:29 BungleBungle 19.7k25 gold badges81 silver badges108 bronze badges 1- Both of your working demo links are broken. Either fix them or remove them please. – dano Commented Aug 15, 2012 at 17:25
2 Answers
Reset to default 3Try:
parent.onscroll = function() { ... };
Update:
Try this, it works for me:
$(parent.document).scroll(function() { ... });
If you are trying to control a parent page which in another domain, so this is not possible for security reasons.
And you can find many articles about this problem, If this is not your case tell us because sure there is another problem.
If in the same domain, what about using this:
$(document).ready(function(){
$(this).parent().scroll(function(){//stuff});
});
$(document).ready(function(){
$(window.parent.document).scroll(function(){//stuff});
});
本文标签: javascriptCan I bind a function to the onscroll event of the parent windowStack Overflow
版权声明:本文标题:javascript - Can I bind a function to the onscroll event of the parent window? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742245969a2439510.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论