8

Is there a way to get response headers of an iframe onload?

I have already googled it, but really I could not find something useful about it!

  • You need to make an ajax request to your iframe url, according to this answer: http://stackoverflow.com/questions/220231/accessing-http-headers-in-javascript – Nelson Jun 22 '13 at 11:20

2 Answers2

3

Not really. If the iframe is on the same domain you can access its document object which contains some useful information such as document.referrer, but you cannot intercept the full HTTP headers without making an Ajax request for the URL. This would mean requesting the URL again. e.g:

$.ajax( { url: $(#myFrame).attr('src'), success: function(r,x){
    console.log( x.getResponseHeader('SomeHeader') );
} } );

This will only work if the iframe src is in the same domain as the calling script.

Tim
  • 8,036
  • 2
  • 36
  • 52
-1

If you have control on the loaded iframe resource you can dump the header content on a postMessage request to the main window.

  • To do that the JavaScript inside the page loaded into the frame would have to read the headers which, as was pointed out [amost a decade ago](https://stackoverflow.com/a/17250188/19068) isn't possible. – Quentin Aug 09 '21 at 10:17
  • You can hard-print all headers as javascript if you have PHP enabled. – KHAYRI R.R. WOULFE Aug 09 '21 at 11:57