0

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.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
neo
  • 313
  • 1
  • 6
  • 19
  • 1
    possible duplicate of [Accessing HTTP Headers in Javascript?](http://stackoverflow.com/questions/220231/accessing-http-headers-in-javascript) – Rafael Feb 20 '12 at 08:18
  • 1
    is there a specific data you need or you just need all? Some data is available in multiple JavaScript objects, like referer is document.referrer – Rafael Feb 20 '12 at 08: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. May 09 '12 at 21:22

2 Answers2

2

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()
   };
}
DG.
  • 3,417
  • 2
  • 23
  • 28
  • 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 ? – neo Feb 20 '12 at 10:25
  • 2
    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. – DG. Feb 20 '12 at 11:24
1

You can use \

document.location.href

It will return the header response

Kunal Vashist
  • 2,380
  • 6
  • 30
  • 59