How to add JavaScript code into existing iFrame using jQuery?

前端 未结 8 2136
借酒劲吻你
借酒劲吻你 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:28

    The problem is that the HTML parser gets confused if your script contains the closing script tag in it () and it closes the script tag prematurely.

    The solution is to escape the / in "<\/script>". This works because strings in JavaScript, (and some other languages), any invalid escape sequences are just ignored, so "\l" is treated as "l", and "\/" is treated as "/". The HTML parser, however, doesn't use a backslash to escape them so it doesn't get confused (credits to https://stackoverflow.com/users/405681/keaukraine).

    var scriptTag = " in their code (jsfiddle):

    var scriptTag = "
    
                                     
                  
提交回复
热议问题