jquery load script on click

烂漫一生 提交于 2019-12-11 07:44:01

问题


I'm attempting to load a javascript script from another site with this code but it isnt working!

    $("#random").click(function(){
        $.getScript("http://www.themoviequotes.com/widgets/javascript?n=1&l=1&g=3");
    });

I have a div with an id of "movie" in which i'm trying to load the content into from the above script!


回答1:


In text/html mode, <script> elements are intrinsically CDATA. Thus &amp; means &amp; and not &.

Do not use HTML entities inside <script> elements.

(If you are writing XHTML and serving it as text/html, then the guidance on XHTML media types with special attention for the section on embedded style sheets and scripts.)

Since you have replaced & with &amp, you are fetching data from a URL that the server doesn't recognize.

However, even if you did change that, it still wouldn't work. The script, which for some reason is being served as text/html instead of application/javascript, contains a document.write statement. This writes to the end of the document (so it is useless for replacing a section of the page) if the document is still open for appending, or replaces it entirely otherwise.

jQuery.load wouldn't help since that expects some content to drop into an element (not a script to execute) and (as far as I know) has no cross-domain capabilities (which only work in very new browsers and only with servers which send the right HTTP headers).




回答2:


Use this:

$("#random").click(function(){
    $.getScript("http://www.themoviequotes.com/widgets/javascript?n=1&l=1&g=3");
});

The URL was wrong.




回答3:


looks like your url is wrong

remove &

http://www.themoviequotes.com/widgets/javascript?n=1&n=l=1&g=3



来源:https://stackoverflow.com/questions/4361033/jquery-load-script-on-click

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!