3

I'm embedding a delicious feed into my page. When I just add the script tags inside the div it obviously loads before the rest of my JS (at the bottom of the page). I'd like to load this script into the specified div dynamically after all other js has loaded.

I've tried the getscript function, but it definitely breaks the page when I use it with .html():

$('#delicious').html(function(){
$.getScript("http://feeds.delicious.com/v2/js/bayonnebridge?title=My%20Delicious%20Bookmarks&icon=m&count=5&sort=date&tags&extended&name&showadd"); 
});

I can't seem to wrap my head around how I can print the results of that file into the #delicious div.

2 Answers 2

3

If you want to display the script in the div, shouldn't it be something like this, making use of the callback function?

$.getScript("http://feeds.delicious.com/v2/js/bayonnebridge?title=My%20Delicious%20Bookmarks&icon=m&count=5&sort=date&tags&extended&name&showadd", 
             function(data){
                $('#delicious').html(data);
            }); 

The result of $.getScript() is the XMLHTTPRequest object and it seems like you want the response text instead.

Also, it looks like the response from the URL you've given attempts to write the script into the document, thus overwriting all your page content. It that is your problem you might consider using an iframe and setting this URL as it's source.

I also seem to get different behaviour depending on whether the script is being loaded cross-domain or from the same domain - I'm not quite sure what to make of that since getScript() is supposed to work cross-domain.

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

1 Comment

Yea, it has me really confused. Putting the script inline works perfectly fine (doesn't over-ride the whole page), I would assume getscript would perform similarly. I may just have to take the performance hit.
1

It sounds like you want to use something like the window load event. Try adding a handler that looks like this:

$(window).load(function() {
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = "http://feeds.delicious.com/v2/js/bayonnebridge?title=My%20Delicious%20Bookmarks&icon=m&count=5&sort=date&tags&extended&name&showadd";
  $('#delicious').append(script);
});

Check out the jQuery .load() API for more information.

4 Comments

This produces the same result - the page loads, but when it hits this js it reloads the whole page and tries to fill it with the url. So weird.
I overlooked your .html() use; you definitely want the response and could append the script dynamically like the example. (However, no.good.at.coding seems to have your answer.) Good luck!
This post may also be helpful to you: stackoverflow.com/questions/4520440/…
Thanks, going to give these a shot in the morning. Appreciate your help!

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.