9

How can I view the page headers of the loaded page using javascript.

Is there something where I can execute a

e.g. window.pageHeaders['session']

Note: I am not talking about a Ajax page load.

Prakash Raman
  • 13,319
  • 27
  • 82
  • 132
  • 1
    Are you talking about the content-headers or the javascript representation of the header-tag or what the header tag contains? – fredrik Aug 14 '12 at 10:52
  • You could do this with [Service Workers](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API), but they're [poorly supported](http://caniuse.com/#feat=serviceworkers) at this time. – Noah Freitas Apr 21 '16 at 01:24

2 Answers2

1

Check out this jsfiddle.

Linking from this question.

This answer by Raja will get the page headers for the current page.

var req = new XMLHttpRequest();
req.open('GET', document.location, false);
req.send(null);
var headers = req.getAllResponseHeaders().toLowerCase();
alert(headers);
Community
  • 1
  • 1
Undefined
  • 11,234
  • 5
  • 37
  • 62
  • This works, but what this does it that it makes an extra ajax call. How can I get the http headers without an extra ajax call ? – Prakash Raman Aug 14 '12 at 11:39
  • @PrakashRaman Do you use any server side language? You can set the headers to a javascript variable then. – Undefined Aug 14 '12 at 12:24
  • That is what I am currently doing, which is my last resort. Was hoping there would be another way. BTW I am using PHP – Prakash Raman Aug 14 '12 at 13:31
-1

you doing something like this? please show us example implementation.

   <!--Save Headers-->
   <?php $currentHeaders = json_encode( getallheaders() ); ?>

   <!--Create Output Container-->
   <div id="output"></div>

   <!--Show Headers-->
   <button onclick="showheaders()">show headers</button>

   <!--Javascript Ugliness-->
   <script>
      function showheaders(){
         var headers = <?php echo $currentHeaders; ?>;

         // Collect your goodies here eg: var goodies = headers['proxy-authenticate'];
         console.log("Headers", headers);

         // See the output on screen?
         var output = document.getElementById('output');
            output.innerHTML = JSON.stringify( headers );
      }
   </script>
4UmNinja
  • 510
  • 4
  • 14