问题
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 &
means &
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 &
, 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