最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Load jQuery in an iframe - Stack Overflow

programmeradmin3浏览0评论

I'm writing a script which requires some stuff to be carried out in an iframe. It's a userscript for a site which I do not own, so because of cross-domain issues, I cannot just set the src of the iframe to a page with my own code. Instead, I am dynamically building the iframe.

To do this, I have

 function childScripts(){
  //Stuff that must be injected into the iframe.
  //Needs jQuery
 }


 //because I'm lazy:
 var iframeWin=$('#my-iframe')[0].contentWindow;

 //Load jQuery:
 var script = iframeWin.document.createElement("script");
 script.type = "text/javascript";
 script.src=".7.1/jquery.min.js"
 iframeWin.document.head.appendChild(script);

 //Inject script
 var script = iframeWin.document.createElement("script");
 script.type = "text/javascript";
 script.textContent="("+childScripts.toString()+")()";
 iframeWin.document.body.appendChild(script);

Now, the injected script needs jQuery, but for some reason, jQuery isn't loaded when the injected script runs--even though jQuery is in <head> and the injected script is in <body>.

I've tried other workarounds--making the injected script run onload. I don't like the idea of setting a timeout and checking for the existence of jQuery, I'd prefer a more elegant solution.

I've also tried copying the $ object to iframeWin.$, but of course that doesn't work, since it just backreferences to the parent $ and manipulates the parent document.

I'm writing a script which requires some stuff to be carried out in an iframe. It's a userscript for a site which I do not own, so because of cross-domain issues, I cannot just set the src of the iframe to a page with my own code. Instead, I am dynamically building the iframe.

To do this, I have

 function childScripts(){
  //Stuff that must be injected into the iframe.
  //Needs jQuery
 }


 //because I'm lazy:
 var iframeWin=$('#my-iframe')[0].contentWindow;

 //Load jQuery:
 var script = iframeWin.document.createElement("script");
 script.type = "text/javascript";
 script.src="http://ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js"
 iframeWin.document.head.appendChild(script);

 //Inject script
 var script = iframeWin.document.createElement("script");
 script.type = "text/javascript";
 script.textContent="("+childScripts.toString()+")()";
 iframeWin.document.body.appendChild(script);

Now, the injected script needs jQuery, but for some reason, jQuery isn't loaded when the injected script runs--even though jQuery is in <head> and the injected script is in <body>.

I've tried other workarounds--making the injected script run onload. I don't like the idea of setting a timeout and checking for the existence of jQuery, I'd prefer a more elegant solution.

I've also tried copying the $ object to iframeWin.$, but of course that doesn't work, since it just backreferences to the parent $ and manipulates the parent document.

Share Improve this question asked May 15, 2012 at 6:26 ManishearthManishearth 16.2k8 gold badges61 silver badges76 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

It's easier to manipulate iframes using jQuery. Please try:

$('<iframe id="my-iframe"/>').load(function(){
  $('#my-iframe').contents().find('body').append('asd').end()
                            .find('body').append('<script src="//ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js"><\/script>').end()
                            .find('body').append('<script>$(function() {alert("hello from jquery");console.log("hello from jquery"); })<\/script>');                             

}).appendTo("body");

Placement:

    <script src="//ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
    <script> 
        $('<iframe id="my-iframe"/>').load(function(){..... 
    </script> 
</body> 
</html>
发布评论

评论列表(0)

  1. 暂无评论