How to fallback to entirely different index page if user has javascript disable?

人走茶凉 提交于 2020-01-15 06:26:24

问题


I have a large, dynamically generated, single page website that heavily relies on javascript. I want to have a fallback for people without javascript.

Each version must have different php code, so basically I need an entirely different index page. I dont want to wrap EVERYTHING in javascript, and EVERYTHING ELSE in tags. Unless there is an easy way of doing that.

What is the best way to load one page or the other depending on whether javascript is enabled or not?


回答1:


First, to directly answer your question, you could redirect the user away from the first index page if JavaScript is enabled using location.href. That is, if the user does not have JavaScript enabled, they will remain on page one. Else they will be redirected to the JavaScript version.

But the way I would generally go about doing this is to first make sure the website works without JavaScript; then add in the extra code for JavaScript after that plain version works.

<a href="foo.php" onclick="return foo();">Foo</a>

Here, if JS is not enabled, they will go to foo.php. Else, you can have the function foo do something and return false, thus keeping them on the page. The same thing can be done for forms.




回答2:


Use the noscript tag to provide the user with a way to the non-javascript page..

example: code at http://jsfiddle.net/xdYNE/ running (when JS off) at http://jsfiddle.net/xdYNE/show/

(hint: see it with JS disabled to understand..)




回答3:


Seems like putting a PHP redirect inside noscript tags would be the simplest way.



来源:https://stackoverflow.com/questions/3936002/how-to-fallback-to-entirely-different-index-page-if-user-has-javascript-disable

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