使用未执行的jquery替换脚本src

时间:2022-12-06 10:45:17

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.

我想在运行时替换脚本的源URL以仅加速开发阶段,因此没有实际的生产用法。这种替换适用于CSS或图像。但是很难让它与javascript src一起工作。虽然通过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):

在Firebug中,目标旧的src url(/old_path_to_file.js)将替换为新的(/ new_path_to_file.js)。所以更换工作。但它报告说,(如果相关,请参见真正的绝对网址http://):

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

无法加载源:http://localhost/mesjs

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

所以脚本src被替换,但没有被执行。我错过了什么,如果这个东西是允许的话呢?

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?

我也试过使用$ .getScript(absUrl + options.newVal +'。js');,但这并没有取代旧的src,只是在dom中添加了更多的j,这不是我想要的。这个部分有什么暗示吗?

Thanks for all possible help.

感谢所有可能的帮助。

2 个解决方案

#1


0  

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.

你想“替换”旧脚本吗?也就是说,用脚本标签加载的javascript不再在内存中,新的javascript是?你不能这样做。您必须取消定义和取消附加由原始脚本执行的事件。

#2


2  

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.

这将添加一个脚本并执行它...你不能替换src并让它运行出于安全原因。您也不能只是“撤消”原始脚本所做的任何事情...基于您的问题我不认为这是一个问题,但如果是,也许您不应该包含初始脚本标记,请确定您需要的内容动态创建脚本元素,如上所示。

#1


0  

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.

你想“替换”旧脚本吗?也就是说,用脚本标签加载的javascript不再在内存中,新的javascript是?你不能这样做。您必须取消定义和取消附加由原始脚本执行的事件。

#2


2  

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.

这将添加一个脚本并执行它...你不能替换src并让它运行出于安全原因。您也不能只是“撤消”原始脚本所做的任何事情...基于您的问题我不认为这是一个问题,但如果是,也许您不应该包含初始脚本标记,请确定您需要的内容动态创建脚本元素,如上所示。