<脚本>是怎样的?在机器级别处理标签?脚本>
我假设浏览器联系网络服务器并请求“src”(在需要 src 的情况下)。
可以以任何方式控制这种交互吗? 服务器可以获得多少关于谁想要 src 以及何时想要 src 的信息?
或者,是否可以故意对请求进行 404 处理,然后控制 404 机制进行适当响应?
I assume the browser contacts the web server and asks for the "src" (in those situations where there's something to src).
Can that interaction be controlled in any way?
How much information is available to the server about who wants the src and when?
Alternatively, could one deliberately 404 the request, then control the 404 mechanism to respond appropriately?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要为此搞乱 404 处理。如果你想动态生成 javascript 就没有必要。
浏览器并不关心
src=""
属性中的 URL 是什么 - 它当然不必以.js
结尾。重要的是,无论 URL 是什么,它都应该返回内容类型为application/javascript
的内容。因此,最简单的方法就是输入 PHP(如果您正在使用的)文件的路径,并确保返回的内容是有效的 Javascript,并使用正确的
Content-Type
HTTP 标头发送Don't mess around with 404 handling for this. If you want to dynamically generate javascript there's no need.
The browser doesn't care what URL is in the
src=""
attribute - it certainly doesn't have to end in.js
. The important thing is that whatever the URL is, it should return its content with the content-typeapplication/javascript
.So the simplest thing to do is put the path to a PHP (if that's what you're using) file and just ensure that what's returned is valid Javascript, sent with a correct
Content-Type
HTTP header通过 src 属性指定的脚本的处理方式与页面上的任何其他资源一样。如果您返回
404
代码,浏览器将不会尝试进一步与服务器处理该资源。服务器获取所有常见的 HTTP 请求信息(假设浏览器正常发送),并将Referrer:
标头设置为包含该资源的页面。Scripts specified via a
src
attribute are handled just like any other resource on a page. If you return a404
code, the browser won't bother trying to deal with the server further for that resource. The server gets all of the usual HTTP request information (assuming the browser sends it normally), and theReferrer:
header is set to the page which is including the resource.