tags:

views:

54

answers:

2

Hi, great people,

I would like to replace the source url of the script on the fly to speed up dev stages only, so no actual production usage. This kind of replacement works well with css or images. But have hard time to make it work with javascript src. The script seems not executed, although loaded via src.

Here is what I have done.

..................
  return this.each(function() {
    $(this).click(function() {
    var t = $(this);
    options.newVal = t.val();
    var absUrl = '/root_path_still_within_the_same_domain/';
    //options.oldVal is just to limit the replacement scope
    var oldJsUrl = absUrl + options.oldVal + '.js';
    var newJsUrl = absUrl + options.newVal + '.js';

    var reJsUrl = $('script[type*=javascript][src*=' + options.oldVal + ']' || 'script[type*=javascript][src*=' + options.newVal + ']');

        if (oldJsUrl.length && options.oldVal.length) {
          reJsUrl.attr('src', newJsUrl);
        }

        options.oldVal = options.newVal;

    });
  }); 

......................

In Firebug, the target old src url (/old_path_to_file.js) is replaced with the new one (/new_path_to_file.js). So the replacement works. But it reports, (see the real absolute url http://, in case relevant):

Failed to load source for: http://localhost/........js

So the script src is replaced, but not executed. Anything I missed, if this stuff is allowed anyway?

I have also tried to use $.getScript(absUrl + options.newVal + '.js');, but this is not replacing the old src, just adding more js to the dom, which is not what I want. Any hint on this part too?

Thanks for all possible help.

A: 

Do you want to "replace" the old script? That is to say, the javascript that was loaded with the script tag is no longer in memory and the new javascript is? You can't do this in this way. You have to un-define and unattach event preformed by the original scripts.

Hogan
So what route should I take if any? Perhaps $.getScript with replacement? Can you provide examples how to un-define and unattach event? Thanks
swan
umm... well I guess I'd have to see what the original script looks like.
Hogan
Each script handling the same things differently:one.js: $('.rounded').corner("8px");two.js: $('.rounded').corner("6px");, etc with more complicated related content. I am trying to avoid merging them into one script with conditions .etc since they need to live separately when the dev is done.
swan
ok, we still need to see exactly what the script does. The way to "replace" the rounded corners, as an example, most efficiently and correctly is not loading another js file.
Hogan
Ok, got the idea. You and Nick said the same thing, but I was expecting too much to see the truth :) I still have to go the old route, conditions if necessary to un-define as you said. Some are simpler like uncorner(:) Thanks for leading me in the right direction.
swan
A: 

If you just want to include another script:

var script = document.createElement("script");
script.src = url; //Specify url here
script.type = "text/javascript";
$("body").append(script);

This will add a script and execute it...you can't replace the src and have it run for security reasons. You also can't just "undo" anything the original script did...based on your question I don't think that's an issue, but if it is, maybe you should not include the initial script tag, decide what you need and dynamically create the script element like I showed above.

Nick Craver
This is like getScript, adding more js to the dom, only not inline. The need on "undo" is actually because the other script is handling the same things differently. So the old memory must be removed. But, can I "undo" the previous loaded script with script.remove(); this way?
swan
@kuswantin - That would remove the script element...but not undo whatever the script has done.
Nick Craver
I see. That explains it. Thanks Nick.
swan