【发布时间】:2011-01-14 15:40:55
【问题描述】:
我想动态替换脚本的源 URL 以加快开发阶段,因此没有实际的生产使用。这种替换适用于 css 或图像。但是很难让它与 javascript src 一起工作。该脚本似乎没有执行,尽管是通过 src 加载的。
这是我所做的。
..................
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;
});
});
......................
在 Firebug 中,目标旧 src url (/old_path_to_file.js) 被新的 (/new_path_to_file.js) 替换。所以替换工作。但它报告,(请参阅真正的绝对网址 http://,如果相关):
无法加载源代码:http://localhost/........js
所以脚本 src 被替换了,但没有被执行。我错过了什么,如果这些东西仍然允许的话?
我也尝试过使用$.getScript(absUrl + options.newVal + '.js');,但这并没有取代旧的src,只是在dom中添加更多的js,这不是我想要的。这部分也有任何提示吗?
感谢所有可能的帮助。
【问题讨论】: