How to add JavaScript code into existing iFrame using jQuery?

前端 未结 8 2123
借酒劲吻你
借酒劲吻你 2020-12-13 08:13

I want to add a JavaScript snippet into an existing iFrame in the page using jQuery. I have the following code...

Code:

content = \"

        
相关标签:
8条回答
  • 2020-12-13 08:33

    You need to escape the /script. It apears.

    Do like google analitcs for instance

     $("#iframe").contents().find("body").append(decodeURI("**%3Cscript%3E** alert(2)  **%3C/script%3E**")); 
    

    replace the script and /script with this escaped ones

    Hope it helps.

    0 讨论(0)
  • 2020-12-13 08:37
    function putScriptInIframes(script, scriptId) {
    
       var $iframes = $('iframe');
       $iframes.each(function () {
            var thisDoc = this.contentWindow.document;
            if ( ! thisDoc.getElementById(scriptID)) {
                var scriptObj = thisDoc.createElement("script");
                scriptObj.type = "text/javascript";
                scriptObj.id = scriptId;
                scriptObj.innerHTML = script;
                thisDoc.body.appendChild(scriptObj);
            }
        });
    }
    

    This was the simplest way I could make it work.

    0 讨论(0)
提交回复
热议问题