编程小贴士

给你的编程提供小点子


取得当前script元素的src(path)

在正常情况下,无defer或async等属性干扰的情况下要准确取得当前script的执行路径
1. MDC里有介绍document.currentScript =》 https://developer.mozilla.org/en-US/docs/DOM/document.currentScript
还能看是否是异步加载还是阻塞加载,确实比较人性化。
2. 在IE下的表现比较出乎我的意料,最后来说明。

没测opera,拍脑袋想应该是支持的吧。

<script type=”text/javascript”>
function getCurrentScriptPath() {
//firefox可以直接得到currentScript
if (document.hasOwnProperty(‘currentScript’)) {
return document.currentScript.src;
}
var scripts = document.scripts || document.getElementsByTagName(‘script’),
len = scripts.length,
state = ‘interactive';
//兼容IE
for (var i=0; i <len; i++) {
if (scripts[i].readyState==state) {
return scripts[i].src;
}
}
//webkit
return scripts[len-1].src;
}
</script>


关于第二个问题吧,网上有_franky同学写的一篇文章讲script readyState问题。
无奈,他怎么每篇blog都写这么多字啊,看得我心急火撩的,写完这笔记再好好看下他写的文章,下次有机会见面也一定和他好好讨论一下这个blog字数的问题。

回到正题,其实就是想说,IE下的script src的方式不一定是“阻塞”的。场景如下:
page.html里引入一个外链的script,a.js
a.js里的代码也是外链两个script,a.a.js与a.b.js。这里的引入办法是用document.write来引入。
a.js代码:

document.write(‘<script src=”a.a.js”><\/script>’);
document.write(‘<script src=”a.b.js”><\/script>’);


此时,我们把所有信息简化成(IE6~IE10)
1. 我们在a.a.js里去看看所有script的readyState的话,是:
a.js — complete
a.a.js — interactive
a.b.js — loaded
2.
a.a.js代码:

var scripts = document.getElementsByTagName(‘script’);
document.title = script[script.length-1].src;
//如果是a.a.js理解起来还很容易 – script阻塞加载嘛
//此时这里IE输出是a.b.js,其他浏览器是a.a.js。


上述的结论告诉我们,其实IE里已经加载了a.js, a.a.js, a.b.js了!
理论知识告诉我们。
a.js是complete可以理解
a.a.js也可以理解
a.b.js是loaded就 不能理解了,谁说script src是阻塞。。。
马后炮的解释也不是不可以 —- 他是IE,就是下载了是下载了,但是还是阻塞运行。

143 Responses to “ 取得当前script元素的src(path) ”

  1. i love pron说道:

    QX1csf Incredible points. Great arguments. Keep up the good spirit.

  2. We stumbled over here different website and thought I may as well check things out. I like what I see so i am just following you. Look forward to exploring your web page yet again.

  3. Tattoo Shop说道:

    Very good info. Lucky me I came across your blog by accident (stumbleupon). I have saved as a favorite for later!

  4. Tattoo Artists说道:

    Thanks a lot for the article.Really thank you! Want more.

  5. Thank you ever so for you article post.Thanks Again. Will read on

  6. drawing说道:

    Wow, fantastic blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your site is great, as well as the content!

  7. This blog is definitely awesome and besides factual. I have picked many interesting things out of this amazing blog. I ad love to go back again and again. Thanks!

  8. Really enjoyed this blog article.Really thank you! Keep writing.

  9. I’а†ve recently started a web site, the info you provide on this website has helped me greatly. Thank you for all of your time & work.

  10. payday loans Your borrower is always to pay back the financial loan amount inside two to four months

  11. Thanks for taking the time to publish this

  12. Click to read说道:

    Incredible mastewq! This blog looks just like my old one! It as on a entirely different subject but it has pretty much the same page layout and design. Outstanding choice of colors!

  13. Very interesting information! Perfect what exactly I wanted!

  14. This can be so wonderfully open-handed of you supplying quickly precisely what a volume

  15. to learn more说道:

    the near future. Anyway, should you have any suggestions or techniques for new blog owners please

  16. hamptonbay说道:

    Nice Site , guys! Great Infos aswell. I bookmarked your site

  17. Clicking Here说道:

    wow, awesome blog article.Really thank you! Fantastic.

  18. visit说道:

    I truly appreciate this article post.Really looking forward to read more. Really Cool.

  19. It as not that I want to replicate your web-site, but I really like the style. Could you let me know which theme are you using? Or was it custom made?

  20. mebel v Tolyatti说道:

    Thank you for the auspicious writeup. It if truth be told

  21. lose weight diet just beneath, are quite a few completely not related websites to ours, nevertheless, they are certainly worth going over

  22. what is mca说道:

    I was able to find good information from your articles.

  23. wow, awesome blog article.Really looking forward to read more. Keep writing.

  24. woking cab说道:

    Its hard to find good help I am constantnly saying that its difficult to find good help, but here is

  25. This blog is obviously educating and also factual. I have discovered helluva useful stuff out of this blog. I ad love to go back every once in a while. Cheers!

  26. We stumbled over here by a different website and thought I should check things out. I like what I see so now i am following you. Look forward to checking out your web page for a second time.

  27. microblog说道:

    Very good blog post. I certainly love this website. Keep it up!

  28. Spot on with this write-up, I really believe that this web site needs a great deal more attention. I all probably be back again to read through more, thanks for the info!

  29. I think this is a real great article post. Great.

  30. This is really interesting, You are a very skilled blogger. I have joined your feed and look forward to seeking more of your excellent post. Also, I ave shared your website in my social networks!

  31. H1B说道:

    Wow, this piece of writing is good, my sister is analyzing these things, therefore I am going to convey her.

  32. lanyard supplier说道:

    Valuable information. Lucky me I found your web site by accident, and I am shocked why this accident didn at happened earlier! I bookmarked it.

  33. Eun说道:

    Wow, amazing blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your website is great, as well as the content!

  34. Nwokolo Collins说道:

    Merely wanna say that this is extremely helpful, Thanks for taking your time to write this.

  35. anchor说道:

    Right now it appears like Drupal would be the preferred blogging platform obtainable at the moment. (from what I ave read) Is that what you are working with in your weblog?

  36. Nwokolo说道:

    Im obliged for the article.Really looking forward to read more. Really Cool.

  37. klm promo code说道:

    Very interesting subject , thanks for posting. Not by age but by capacity is wisdom acquired. by Titus Maccius Plautus.

  38. you ave gotten an ideal weblog right here! would you like to make some invite posts on my weblog?

  39. Im obliged for the blog.Really looking forward to read more. Keep writing.

  40. other hand I will certainly come again again.

  41. mp3cutters说道:

    Tirage divinatoire gratuit amour l horoscope d aujourd hui

  42. truyen ngon tinh说道:

    P.S My apologies for getting off-topic but I had to ask!

  43. I really liked your blog post.Much thanks again. Really Great.

  44. Very good blog article.Really thank you! Great.

  45. Wow, awesome blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your site is fantastic, as well as the content!

  46. Yo dude! Look up at the skies NATO is spraying fake clouds that are very toxic most clouds are not natural anymore, please research you will thank me for bringing this to your attention. PEACE.

  47. may you be rich and continue to guide other people.

  48. shein promo code说道:

    This is very interesting, You are a very skilled blogger. I ave joined your rss feed and look forward to seeking more of your fantastic post. Also, I have shared your website in my social networks!

  49. Tattoo Shop说道:

    Im thankful for the blog post.Much thanks again. Keep writing.

  50. visit website说道:

    put this information together. I once again find myself spending a lot of time both reading and commenting.

  51. decking wood说道:

    Thanks for sharing, this is a fantastic post.Really looking forward to read more. Really Cool.

  52. Well I really liked reading it. This article provided by you is very useful for accurate planning.

  53. sidekick说道:

    wonderful write-up It as possible you have stated a number of excellent elements, thanks for the post.

  54. Supermodel Elena说道:

    Very informative blog.Really thank you! Much obliged.

  55. this website说道:

    I think you have observed some very interesting details , regards for the post.

  56. this website说道:

    Some really good information, Sword lily I discovered this. What you do speaks therefore loudly that i cannot hear that which you say. by Ron Waldo Emerson.

  57. money说道:

    Thanks for sharing, this is a fantastic blog article.Really looking forward to read more. Much obliged.

  58. women说道:

    You ave made some decent points there. I checked on the net for more information about the issue and found most people will go along with your views on this web site.

  59. You have mentioned very interesting points! ps decent web site.

  60. Wow, great post.Really looking forward to read more. Fantastic.

  61. internet. You actually know how to bring an issue to light and make it important.

  62. Womens Ray Ban Sunglasses Womens Ray Ban Sunglasses

  63. Crochet bolero说道:

    seo tools ??????30????????????????5??????????????? | ????????

  64. Tejidos说道:

    Subscribe to online newsletters from the major airlines. The opportunity savings you all enjoy will a lot more than replace dealing with more pieces of your email address contact information.

  65. This is very interesting, You are a very skilled blogger. I ave joined your feed and look forward to seeking more of your magnificent post. Also, I have shared your web site in my social networks!

  66. online说道:

    website and detailed information you provide. It as good to come

  67. blue nike说道:

    Just to let you know your blog appears a little bit unusual in Firefox on my pc with Linux.

  68. value说道:

    we ad like work out extra techniques in this regard,

  69. Really informative blog article.Really looking forward to read more. Awesome.

  70. kouponkabla.com说道:

    Those concerned with privacy will be relieved to know you can prevent the public from seeing your personal listening habits if you so choose.

  71. School admission说道:

    Say, you got a nice article post.Really thank you! Awesome.

  72. School news说道:

    Keep up the good piece of work, I read few posts on this internet site and I conceive that your website is really interesting and has sets of good information.

  73. ps4 ark servers说道:

    Timon had the gifts of God, which is known for, plus an atmosphere pouch at the back

  74. This is my first time go to see at here and i am in fact happy to read all at single place.

  75. Brandon Moss说道:

    Wanted to drop a remark and let you know your Feed isnt working today. I tried adding it to my Yahoo reader account but got absolutely nothing.

  76. Say, you got a nice post.Thanks Again. Keep writing.

  77. Really enjoyed this blog article.Really looking forward to read more. Fantastic.

  78. Moskito说道:

    light bulbs are good for lighting the home but stay away from incandescent lamps simply because they produce so substantially heat

  79. click here说道:

    the Country/Roots and Americana charts in both

  80. I truly appreciate this blog.Really thank you! Want more.

  81. Thanks for the blog post.Much thanks again. Great.

  82. Terrific work! This is the type of information that should be shared around the web. Shame on Google for not positioning this post higher! Come on over and visit my web site. Thanks =)

  83. Whoa! This blog looks exactly like my old one! It as on a totally different subject but it has pretty much the same layout and design. Outstanding choice of colors!

  84. You certainly understand how to bring a problem to light

  85. This blog was how do you say it? Relevant!! Finally I ave found something that helped me. Thanks a lot!

  86. I regard something really interesting about your web site so I saved to fav.

  87. Really enjoyed this blog post. Really Cool.

  88. tips说道:

    Thank you ever so for you blog post.Really thank you! Awesome.

  89. hotels说道:

    I’а†ve recently started a website, the information you provide on this web site has helped me tremendously. Thank you for all of your time & work.

  90. pretty useful material, overall I imagine this is worth a bookmark, thanks

  91. very nice post, i in fact really like this internet website, maintain on it

  92. Volutpat lacinia dui quis Curabitur Pellentesque ipsum eu

  93. see the website说道:

    I was recommended this web site by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my difficulty. You are incredible! Thanks!

  94. wow, awesome post.Really looking forward to read more. Cool.

  95. lawyer说道:

    Red your site publish and loved it. Have you at any time thought about guest posting on other related blogs related to your blog?

  96. visit website说道:

    That is a great tip especially to those fresh to the blogosphere. Simple but very precise information Many thanks for sharing this one. A must read article!

  97. Really enjoyed this blog article.Much thanks again. Fantastic.

  98. You have brought up a very excellent points , appreciate it for the post.

  99. iHerb Malaysia说道:

    Thanks for sharing, this is a fantastic article post.Really looking forward to read more. Really Cool.

  100. Winners说道:

    writing then you have to apply these methods to your won website.

  101. Erin说道:

    provide whether post dated check or authorize the borrowed funds company to electronically debit the total amount from your bank checking account.

  102. wonderful points altogether, you simply gained a emblem new reader. What might you suggest about your post that you simply made a few days in the past? Any certain?

  103. like this说道:

    montre divorcee cette maman positions fait bien les coups sans cul image avec et recevoir

  104. Wow! Thank you! I permanently wanted to write on my site something like that. Can I include a part of your post to my blog?

  105. them towards the point of full аЂа‹sensory overloadаЂа›. This is an outdated cliche that you have

  106. Loving the info on this web site , you have done great job on the posts.

  107. whites are thoroughly mixed. I personally believe any one of such totes

  108. you ave got a fantastic blog right here! would you wish to make some invite posts on my weblog?

  109. Thanks for another great article. Where else may anybody get that kind of info in such a perfect means of writing? I have a presentation subsequent week, and I am on the look for such information.

  110. Share now说道:

    This awesome blog is definitely interesting and besides informative. I have found helluva handy advices out of it. I ad love to return every once in a while. Thanks!

  111. to read more说道:

    Very informative post.Much thanks again. Cool.

  112. Where can I contact your company if I need some help?

  113. free advertising说道:

    pretty helpful material, overall I imagine this is really worth a bookmark, thanks

  114. to actually obtain valuable facts concerning my study and knowledge.

  115. for more info说道:

    Wow, amazing blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your web site is fantastic, let alone the content!

  116. have a look at说道:

    There as noticeably a bundle to know about this. I assume you made sure nice factors in features also.

  117. Whispering Misty So sorry you can expect to skip the workshop!

  118. Nice! Just wanted to respond. I thoroughly loved your post. Keep up the great work.

  119. caratulas CD说道:

    This is very interesting, You are a very skilled blogger. I ave joined your rss feed and look forward to seeking more of your great post. Also, I have shared your website in my social networks!

  120. web说道:

    Very interesting details you have mentioned , regards for putting up.

  121. Really informative post.Much thanks again. Awesome.

  122. Right now it appears like WordPress is the top blogging platform out there right now. (from what I ave read) Is that what you are using on your blog?

  123. Very good information. Lucky me I found your site by accident (stumbleupon). I ave bookmarked it for later!

  124. tu van tinh yeu说道:

    This is one awesome article post. Really Great.

  125. You could certainly see your skills in the work you write. The sector hopes for even more passionate writers such as you who are not afraid to say how they believe. Always go after your heart.

  126. Thank you ever so for you article.Thanks Again. Keep writing.

  127. check here说道:

    So you found a company that claims to be a Search Engine Optimization Expert, but

  128. go there说道:

    your weblog? My website is in the very same area of interest as yours and my visitors would really benefit from a lot of the information

  129. that I have really enjoyed browsing your blog posts.

  130. You got a really useful blog I have been here reading for about an hour. I am a newbie and your success is very much an inspiration for me.

  131. Really enjoyed this article post.Thanks Again.

  132. I wanted to thank you for this excellent read!! I absolutely loved every little bit of it. I have you book-marked to look at new stuff you post

  133. I appreciate you sharing this post.Really looking forward to read more. Fantastic.

  134. Nice post, thanks. Could you explain the third paragraph in more detail?

  135. my portfolio说道:

    My brother suggested I might like this web site. He was entirely right. This post actually made my day.

  136. click here说道:

    wow, awesome post.Really thank you! Really Great.

  137. good morning说道:

    I think this is a real great blog.Much thanks again. Will read on

  138. You acquired a really useful blog page I have been here reading for about an hour. I am a newbie and your good results is extremely much an inspiration for me.

  139. Foulard说道:

    Within the occasion you can email myself by incorporating suggestions in how you have produced your web site search this brilliant, I ad personally have fun right here.

  140. wow, awesome blog article.Really thank you! Want more.

  141. Thanks for sharing, this is a fantastic blog post.Really thank you! Great.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>