Hi Ricardo,

ricardobeat wrote:

This works though: $('head')[0].appendChild($('<script type="text/
javascript" src="what.js">')[0])


Thanks, that is pretty slick. Much better than the approach I took of tracking scripts by appending place holder tags in head.

Just to summarize, my approach to ensure javascript and stylesheet resources are only loaded once, is to validate against guard functions before appending the resource.

An example guard function:

/*
 * This function checks if the given script already exists in the
 * document head section. It uses the following two rules:
 *
 * External scripts are checked against their 'src' attribute.
 * Inline scripts are checked against their 'id' attribute.
 *
 * @param script is a <script> DOM element.
 */
function canAddScript(script) {
  var id = script.getAttribute("id");
  if(id) {
    // Check if another script exists with the same id attribute
    if($('#'+id).length) {
      log('script with id: "' + id + '" already exists');
      return false;
    }
  }
  var src = script.getAttribute("src");
  if (src) {
    // Check if another script exists with the same src attribute
    if($('script[src='+src+']').length) {
      log('script with src: "' + src + '" already exists');
      return false;
    }
  }
  return true;
}


kind regards

bob



On Oct 29, 9:11 pm, Bob Schellink <[EMAIL PROTECTED]> wrote:
Hi Ricardo,

ricardobeat wrote:
You don't need a plug-in to do that:
$('head')
   .append('<link href="css/geral.css" rel="stylesheet" />')
   .append('<script type="text/javascript" src="calendar.js"></
script>')
Using the above method (or taconite) I've noticed that JQuery does not
actually append the <script> element to head. At least according to
Firebug. Is this the expected behavior?

One of the things I want to achieve is that scripts should only be
appended once for a page. However if JQuery does not add the script to
head there is nothing to check against in a future call which might
add the same script again.

Should I bypass JQuery and manually add the script tags to head?

kind regards

bob


Reply via email to