简体   繁体   English

如何在JavaScript中获取scripttag url标头值

[英]How to get the scripttag url header values in javascript

I have html page with script tag. 我有带脚本标签的html页面。 In that script tag I am going to load external url, like.. 在该脚本标签中,我将加载外部网址,例如。

<script src="http://www.test.com" />

Is it possible to read that url ( http://www.test.com ) header values by javascript. 是否可以通过javascript读取url( http://www.test.com )标头值。

Note: That script tag external url have iframe restriction. 注意:该脚本标记外部url具有iframe限制。

If you are using a .js file, then it can't have Header values. 如果您使用的是.js文件,则该文件不能包含Header值。

Normally you can get the Header values via JavaScript by using Ajax Request. 通常,您可以使用Ajax Request通过JavaScript获得Header值。

for example: 例如:

 var url = "www.test.com"; var rh; // Response Headers var xhr = new XMLHttpRequest(); xhr.open("GET",url,true); xhr.send(); xhr.onreadystatechange = function(){ if(xhr.status == 200 && xhr.readyState == 4){ rh = xhr.getAllResponseHeaders(); } } // now rh contain all headers //or you can make a HEAD request which return all HEADER elements of the URL xhr.open("HEAD", url,true); xhr.onreadystatechange=function() { if (xhr.readyState==4) { rh = xhr.getAllResponseHeaders(); } } xhr.send(null) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM