Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically loading jQuery and extensions

I'm trying to encapsulate a javascript that I built using jQuery, jQuery UI, jQuery Form and xmlDom.

I want to be able to send to my client just one javascript that references the other ones, including just a small piece of js for options settings.

Find below an example:

<script language="javascript">
var myOptions = {
   shop: 1,
   style: "gold"
}
load();
</script>
<script src="http://myServer/myScript.js" type="text/javascript"></script>

It's been nearly impossible to me to load jQuery and plugins dynamically. I read a lot of examples, jQuery works fine when I load it dynamically, but the rest of the extensions never worked properly. Following sequence is the unique one that loaded at least a couple of plugins:

Set a load function that loads jQuery dynamically:

load = function() {
    load.getScript(url_base + "/js/jquery-1.3.2.js");
    load.tryReady(0); 
}
load.getScript = function(filename) {
  var script = document.createElement('script')
  script.setAttribute("type","text/javascript")
  script.setAttribute("src", filename)
  if (typeof script!="undefined")
  document.getElementsByTagName("head")[0].appendChild(script)
}
load.tryReady = function(time_elapsed) {
  // Continually polls to see if jQuery is loaded.
  if (typeof $ == "undefined") { // if jQuery isn't loaded yet...
    if (time_elapsed <= 5000) { // and we havn't given up trying...
      setTimeout("load.tryReady(" + (time_elapsed + 200) + ")", 200); // set a timer to check again in 200 ms.
    } else {
      alert("Timed out while loading jQuery.")
    }
  } else {
    ...
  }

Load every plugin after that, jQueryForm, jQuery UI, xmlDom

When I check for jQuery form it's available:

if (jQuery().ajaxForm)

When I check for xmlDom it works.

When I check for jQuery UI it's never available.

if(jQuery().ui)

It doesn't matter if I set a timeout to wait for UI, it's never loaded. Seems to be jQuery executes my UI features before it's loaded.

Can anybody send me a link to help with this?

like image 321
Maximiliano Rios Avatar asked Dec 27 '09 17:12

Maximiliano Rios


People also ask

How to dynamically load a JavaScript file with jQuery?

Dynamically load a Javascript file with jQuery jQuery’s $.getScript () function is a shorthand wrapper to $.ajax () which easily allows remote Javascript to be loaded into the current page.

How to load page content from database using jQuery?

Page title as navigation menu and home page content are loading here by default. On click of menu link calling jQuery function getContent () for AJAX request and loading page content from database in load_content.php page. On success, response is loading in <div id=”content”></div>

How to load dynamic output by Ajax request in WordPress?

Loading page title as navigation menu and displaying div for loading dynamic output by AJAX request. In this function passing page id using AJAX request on “load_content.php” page and retrieve content of matching id from database and load this content in output content div area after successful response.

How to optimize web page performance with on-demand loading?

It allows you to optimize web page performance by moving blocking scripts off the loading process (usually they call this “ lazy loading ”), and to load scripts only when the user needs them (usually they call this “ on-demand loading ”.) This tool will greatly improve your page performance if you use it wisely.


1 Answers

Perhaps, this might help. From the dive.into.javascript site:

SidJS is a lightweight JavaScript library used to load JavaScript scripts and CSS stylesheets on demand. It increases AJAX applications performance by loading resources when they're needed.

Minified with the Google Closure Compiler, it weighs in at 1.12KB (625 bytes when GZipped).

If you absolutely cannot include another javascript library, you can take a look at the code (there isn't much of it, really); with the ideas in there, you should be able to fix your script.

EDIT
If you use the Google Closure Compiler to minify the script, you'll need to add back the node.sheet.cssRules line. It appears to be used in Gecko and Webkit browsers, to test if a lazy-loaded stylesheet has been completely downloaded (of course, if you won't be lazy-loading stylesheets, you can remove that section completely).

like image 188
elo80ka Avatar answered Nov 06 '22 11:11

elo80ka