1

Is there a way to read the HTTP header with jQuery without performing a .ajax request and reading xhr.status?

Along similar lines to the popular Google Analytics addon (gaAddons) I wish to track 404s on my site. Unfortunately, I have inherited a legacy system and 404s do not have a unique URL.

Is it possible to read the HTTP header using jQuery?

crmpicco
  • 16,605
  • 26
  • 134
  • 210
  • 3
    Do you want to know whether the *current* page is a 404 page? Or are you trying to find out if a different URL returns a 404 page without actually making a request to that URL? – Gareth Aug 03 '12 at 10:32

4 Answers4

0

I don't think this is possible - they are not exposed to JavaScript. An AJAX request is the obvious workaround - not sure why you're against this.

More info in this question: Accessing the web page's HTTP Headers in JavaScript

Community
  • 1
  • 1
Mitya
  • 33,629
  • 9
  • 60
  • 107
0

If you have access to the 404 error page, you could inject javascript into the 404 page that can send page details like, url, time etc to server through ajax [but it's point less if they are in server logs]. But then, this script can be exploited.

Prasanth
  • 5,230
  • 2
  • 29
  • 61
  • If I go to `www.ayrshiremins.com/abcdefg` this page doesn't exist, so I will be redirected (in My PHP) to `www.ayrshireminis.com/error`. This error page is used for other purposes other than 404s so how would you suggest I track the 404? – crmpicco Aug 03 '12 at 10:53
  • If you could see if abcdefg is a valid page, then when you are redirected to error, check if referrer is a valid page. if it's not, log the referrer. – Prasanth Aug 03 '12 at 10:59
0

You might be able to do something like this in jQuery:

if ( $('meta[content="Not Found - The URL you requested could not be found."]').length>0 ) {
    /* do something about it */
}

This assumes that the delivery engine has put the following meta tag in the header of the page. Tumblr does this, for example. Your own system may put a different meta tag or you could design your system to deliver some such meta tag.

<meta name="description" content="Not Found - The URL you requested could not be found." />

Of course, all this is really doing is checking the meta header which offers a page summary. But you could take advantage of the mechanism in your own environment.

Octopus
  • 8,075
  • 5
  • 46
  • 66
0

While doing site analytics i had to record errors separately.

Similar to @Octopus suggestion i added a meta tag to 404 and 500 pages.

<meta name="error" content="404">

On the client i then checked if this meta tag existed like so:

<script>
  meta = document.getElementsByTagName('meta');

  for (i=0; i< meta.length; i++) {

    if(meta[i].name == 'error'){
       alert('This page returned a ' + meta[i].content + ' error');   
    }

  }

</script>

Unfortunately i had to use vanilla JS to achieve this.

sidarcy
  • 2,958
  • 2
  • 36
  • 37