views:

45

answers:

3

I want to load the lightbox javascript only when a certain condition is satisfied so I'm loading it using $.ajax like so:

$.ajax({ url: "../static/js/lightbox.js", dataType: 'script', cache: true, success: function() {
    alert('loaded');
    $("a.lightbox").lightbox({
        opacity: "0.6",
        width: "940"
    });
}});

I see the "loaded" alert but the lightbox does not work. However, when I load the file directly (script src) from the HTML, lightbox works. How do I fix this?

Many thanks for your help.

+5  A: 

You'll want to use $.getScript() for this (shorter, but slightly different caching effect), for example:

$.getScript("../static/js/lightbox.js", function() {
  $("a.lightbox").lightbox({
    opacity: "0.6",
    width: "940"
  });
});
Nick Craver
+1 for `$.getScript` did not know about that :)
Web Logic
getScript is a shorthand to $.ajax( {url:url, dataType:'script'})
korchev
getScript is the same, just that it doesn't support asynchronous caching.
Nimbuz
...and btw, its the same with getScript.
Nimbuz
@Nimbuz - The caching you don't want here, you'll get duplicate script includes or no success callback with the ajax syntax. Try `alert($.fn.lightbox)` what do you get?
Nick Craver
@Nimbuz - In that case your lightbox should be working...are you expecting it to open instantly? lightbox doesn't open automatically by default, just attaches a click handler.
Nick Craver
@Nick - Okay, so it doesn't work with getScript either. I wonder whats wrong!
Nimbuz
Yes, lightbox should open instatly. If I load the script directly, lightbox works, but not with getscript of ajax.
Nimbuz
@Nimbuz - What if you do `$("a.lightbox").lightbox({ opacity: "0.6", width: "940" }).trigger('click');` in that callback?
Nick Craver
Nope, doesn't work still. I have placed an alert ("yay") in lightbox.js to check if it works, I get 'undefined' right now using getScript.
Nimbuz
@Nimbuz - Try `$.getScript("../static/js/lightbox.js", function(script) { alert(script); });`, do you see the text of the lightbox script?
Nick Craver
Nope, just a blank alert in Chrome and NO alert in Firefox.
Nimbuz
@Nimbuz - The script isn't coming back then...do you have a page we can see? Without seeing firebug output or something else, it's a blind guess as to why the request doesn't return....the url you posted is correct right? You're not fetching the script from another domain or anything?
Nick Craver
Okay, strange! I created a test file with the same setup, it doesn't work on my mac, as expected, but works fine on my webhost (http://bit.ly/cDPlCs). I have no idea whats causing it and how to fix! :(
Nimbuz
Screenshot of directory structure: http://bit.ly/azw6Mp
Nimbuz
@Nimbuz - Locally you're subject to the [same origin policy](http://en.wikipedia.org/wiki/Same_origin_policy) if you're dealing with `file://` instead of `http://localhost/`, which won't allow scripts (or anything else) to be fetched via ajax.
Nick Craver
+1  A: 

Does the script get properly downloaded? Do you see the 'loaded' alert? If I were you I would put an alert in lightbox.js to see if is properly executed after downloading. Also check for JavaScript errors.

korchev
Great idea! But no, the alert from lightbox.js does NOT load. :(
Nimbuz
Is the file downloaded at all? It could be an issue due to wrong path. You can use Fiddler of Firebug to see if the HTTP request is successful or not
korchev
A: 

hi, The issue is the js is not still part of the DOM. Its correct that you get the lightbox.js but its not ready for execution. To get a js file dynamically, you will have to use getScript method instead of ajax. Have a look at the API: http://api.jquery.com/jQuery.getScript/

Abdel Olakara