1

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!

4
  • do you think the url is correct for sure??? i typed it directly and it didn't return me any thing?? Commented Dec 5, 2010 at 20:44
  • absolutely, this the current HTML: <div id="movie"> <script src="themoviequotes.com/widgets/…" type="text/javascript"></script> </div> Commented Dec 5, 2010 at 20:44
  • 1
    That URL returns an empty page. Commented Dec 5, 2010 at 20:46
  • change the url , its working now , remove &amp;stuff...and make it proper querystring Commented Dec 5, 2010 at 20:49

3 Answers 3

2

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).

Sign up to request clarification or add additional context in comments.

4 Comments

That makes sense, am i using getScript correctly, should i use load(), i'm trying to replace the current content with this content everytime the user presses "random"
@ben , getscript just gets the javascript from the specified location and adds to head section
Ok, thanks for that, at the moment, it works but the page has to be refreshed. Is there no way i could get new content from this URL without refreshing the page
Not from the client. You could proxy the data through a program running on your own server, and then use jQuery.load
0

Use this:

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

The URL was wrong.

Comments

0

looks like your url is wrong

remove &

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.