11

I have a static HTML web page and I want to set its status code to 404 "Not found".

But, I want to do this using javascript or jQuery, not any server side language.

How can I do this on the client? With javascript or maybe a <meta> tag?

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • What's the purpose of this? I hate 404's, so when a page is available, rejoice! – Marcel Korpel Dec 22 '10 at 12:25
  • i am try to do this in javascript because it is interesting and more when the limit set to javascript only –  Dec 22 '10 at 14:09
  • 1
    It isn't interesting, it is nonsensical. A 404 means "That thing you requested via HTTP does not exist". It only makes sense as part of an HTTP response header. – Quentin Dec 22 '10 at 15:54
  • I can imagine that on a single page site, dealing with a REST situation that is not supported, you would like to do something like setting a 404 and expect the browser to deal with default 404 behavior. Nevertheless I have to agree with Marcel that you can deal with the situation because you can show a custom (client javascript generated) 404 page, helping your users to return to the proper flow of your site. – Jos Dec 10 '13 at 10:24

4 Answers4

21

It is impossible to set the HTTP Response code using client side programming.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
4

You can redirect the page to a non existent one in JavaScript. I have no idea why you would want to do this, but hey ho:

<script type="text/javascript">
<!--
window.location = "zomgomgthispagedoesntexistlololol.html"
//-->
</script>

But David is right, there is no way to set THAT pages status as it is a client side script.

Tom Gullen
  • 61,249
  • 84
  • 283
  • 456
4

I think the sense behind this exercise is the following: You want to load a page that displays a custom error message to your users. At the same time you want to tell your Google Search Appliance (or alike systems) that the requested page must be deleted from the index. I have the same problem where server-side scripts cannot be embedded. I haven't found a client-side solution yet.

0

You set redirection for non-existant pages on the server. You will not have a page to work with in the browser, since the page the user requested does not exist.

In ASP.NET you can set up a default page in the customErrors section of the web.config file like this:

<customErrors mode="RemoteOnly" defaultRedirect="~/error.aspx">
  <error statusCode="404" redirect="notFound.aspx"/>
  <error statusCode="403" redirect="accessError.aspx"/>

Chris Love
  • 3,740
  • 1
  • 19
  • 16