开发者

jQuery dynamic script tag addition causes IE9 to request src twice

Just came across something really weird with IE9 (how unusual?)

I have the following code to overcome cross domain restrictions and load a javascript that runs on localhost

 var url = 'http://local.maglnk.开发者_如何学运维com:45099/download/?info_hash='+info_hash;
 var scriptTag = $('<script />').attr('src',url);
 scriptTag.appendTo('body');

This code works fine on any browser other than IE9, the weird thing with it is that IE9 requests the url of the script tag twice, and the second time that it does it it appends some sort of timestamp at the end of the URL

 http://local.maglnk.com:45099/download/?info_hash=6eabb12b8bf344feba7d323c940c18d096771b99&_=1309998922080)

I'm not sure if the trailing "&_=1309998922080" is added by jQuery when it appends the script tag dynamically, or if IE is doing some sort of pre-fecthing or pre-parsing in which case I come to ask here if there's a way to stop that mechanism via javascript.

If you've ever come across something like this please drop a line.


If you are using jQuery anyhow, why not use jQuery.getScript?


Found the solution.

Do not use the appendTo() jQuery method. Instead use $.getScript(url) and the issue goes away.

So, it seems like it's a jQuery issue with IE9 when you dynamically add script tags.

Cheers.


In some cases $.getScript() might not be suitable, because it can mess up the order of the loaded javascript files. That's because it is asynchronous. If the dynamically loaded JavaScript files are dependencies of each other, that would cause issues.

In that case, you'll want to do this:

$( document.body ).append( $( '<script src="hello.js"></script>' ) );

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜