JavaScript标记的src属性会在所有浏览器中遵循HTTP重定向吗? [英] Will JavaScript tag's src attribute follow HTTP redirects in all browsers
问题描述
比方说,javascript标记的src属性指向一个重定向:
< script src =http:// foo.com/foo.js\"></script>
其中 http://foo.com/foo.js 是一个301重定向到 https:// foo.com/foo.js ...
所有的浏览器能否成功加载JS文件?我注意到它似乎可以在Chrome,Firefox,Safari和IE9中运行......但是我只是好奇它是一种规范还是随机的...
这里唯一要注意的是浏览器发出两个请求来下载一个资源&前提是脚本调用在浏览器中被阻止,所以不建议长时间使用此策略。我们使用301的3个非常基本的原因是:
- 美化网址
- 确保链接权益
- 解决规范问题。
Let's say, a javascript tag's src attribute points to a redirect:
<script src="http://foo.com/foo.js"></script>
where http://foo.com/foo.js is a 301 redirect to https://foo.com/foo.js...
Will all browsers successfully load the JS file? I've noticed it seems to work in Chrome, Firefox, Safari, and IE9... but I'm just curious if this is something that's in a spec or just random...
Loading resources for a webpage (be it script source, image source or whatever) is agnostic to how browser fetches it for you (using HTTP protocol over TCP/IP).
The only thing to be aware of here is that browser makes two request to download one resource & provided that script calls are blocking in browser, so it is not advised to use this strategy for long. For the 3 very basic reason we use 301s are:
- Prettify URLs
- Ensure Link equity
- Resolve canonical issue.
这篇关于JavaScript标记的src属性会在所有浏览器中遵循HTTP重定向吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!