开发者

Reload <head> using jQuery

开发者 https://www.devze.com 2023-03-07 15:51 出处:网络
Okay, so I know this is very weird and abnormal and I should probably avoid it, but this is what I want to do.I basically want to reload every <script> in the header through jQuery. 开发者_高级运

Okay, so I know this is very weird and abnormal and I should probably avoid it, but this is what I want to do. I basically want to reload every <script> in the header through jQuery. 开发者_高级运维 You can use .load(url+" #element"), but that's only for elements with an ID not with a tag.

How can I select and reload all the scripts in a document?

Thanks


As I understand it, what you're trying to do is load more JS onto the page. How about fetching the contents of the JS file (as a string) using AJAX (with say PHP as the backend) and then executing it on the client side using Javascript's eval() function.


Your best bet is to just re-add every script tag in the head.

$('head script').each(function(idx,el){
   var pSrc = $(el).attr('src');
   if (pSrc)
   {
      $('head').append('<script src="'+pSrc+'"></script>');
   }
});

I did a cursory test, and it seemd to work fine.


I found an alternate solution to the problem and took the long route just to be safe. Thanks for the answers guys. Definitely some useful info in here.

0

精彩评论

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