Document write gives strange output

前端 未结 2 724
一向
一向 2021-01-25 01:41

I\'m writing a script to detect jQuery, if it doesn\'t exist then insert the Google CDN version and a local fallback (don\'t ask why... it\'s not my idea), the problem is when I

2条回答
  •  孤城傲影
    2021-01-25 02:23

    Basically, what's happening is that when document.write prints out

    ')
    

    that first is being parsed into the actual end-of-script tag, even though it's inside of a string, resulting in something like

    
    ')
    
    

    The string is not ended (unterminated string literal) because its closing single quote is now outside of the script, and there is also a dangling end-of-script tag. To stop this from happening, you simply need to escape like crazy the script tags inside the string, especially in the string inside the string. Below is a working example.

    document.write("
    
                                     
                  
提交回复
热议问题