Can I change the page's HTML status code on the client side?

让人想犯罪 __ 提交于 2020-01-03 12:58:52

问题


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?


回答1:


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




回答2:


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.




回答3:


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.




回答4:


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"/>




回答5:


I'm currently faced with issue. I knew it wouldn't be possible but thought I'd give it a look anyway. Reason I'm facing this is because of vue-router and the limitations it has in history mode. They suggest setting a server-side configuration that catches all URLs that could be a client-side route-able and returning the root/index.html for the client-side routing to take over. And then another catch-all client side if the route doesn't exist there. What they fail to explain is how you can report such a failing to crawlers/indexers with the real status code. With this solution you just get 200s going to any requestors even though the page is not found.

https://router.vuejs.org/en/essentials/history-mode.html



来源:https://stackoverflow.com/questions/4508913/can-i-change-the-pages-html-status-code-on-the-client-side

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!