Is there a way to get the HTTP header information of the current page in JavaScript?
I am trying to get the header information of a page like referer and other headers. How can I get those values in a JavaScript function so that I can send that information to a Java applet? The problem while I am making an Ajax call and getting header information is the referer will change to current page than the original referer.
-
1possible duplicate of Accessing HTTP Headers in Javascript?Rafael– Rafael2012-02-20 08:18:29 +00:00Commented Feb 20, 2012 at 8:18
-
1is there a specific data you need or you just need all? Some data is available in multiple JavaScript objects, like referer is document.referrerRafael– Rafael2012-02-20 08:23:04 +00:00Commented Feb 20, 2012 at 8:23
-
Don't forget to accept someone's answer (click the check mark next to the answer), or at least give them a vote up (click the gray up arrow), for spending time to answer your question.DG.– DG.2012-05-09 21:22:13 +00:00Commented May 9, 2012 at 21:22
Add a comment
|
2 Answers
Use document.referrer for referrer. Use Ajax to get the rest.
function getHeadersAjax(url) {
var r = GetXmlHttpObject();
r.open('HEAD', url, false);
r.send(null);
return {
status: r.status,
statusText: r.statusText,
referrer: document.referrer,
rawheaders: r.getAllResponseHeaders()
};
}
2 Comments
neo
I already loaded a page and i want to get the information of that page. in this case will it rewrite the information ? i mean to say the referrer to that page and other details ?
DG.
You have to be specific about what headers you want, but I can tell you that some headers are only available by using ajax to fetch the same page a 2nd time. In my example, use
url = document.location.href. Please keep in mind that I am talking about headers sent by the server. If you want headers sent by the browser, you must use a server page that will reflect that info back.