I am developing an application that uses javascript to perform some arithmetical calculations in forms. I would like to check if javascript is enabled in user's browser. If it is disabled redirect the user to warning page asking to enable javascript. I am aware of <noscript>
tag. It is not enough just to display the message for me. I do not want to display the forms that requires javascript enabled. I do not like get_browser()
function because it requires browscap.ini file that is not bundled with PHP. I would not like to remember about this file when I install application on another server. Is there any other good way to check whether javascript is enabled?
views:
138answers:
5The other way around is much simpler. Your page defaults to showing "Please enable Javascript", and the first thing you do in your Javascript is to remove that message and do whatever you need to do.
No, but you can simply get javascript to add the forms on the 'load' event, this should do the same thing.. (edit: meaning you have the error message embedded in the page, then replace it with the form via javascript)
The best way is noscript and display a message saying that he/she needs javascript enabled. You say you want to redirect. However on the client side it is impossible to redirect without javascript (HTTP redirects are available, but are not cancellable once started, meaning if there was javascript, it would still redirect). There is also no way for a web server to know if the browser actually has javascript enabled or not. You can check to see if the browser in general has support for javascript, but the user can always turn javascript off.
Depending on your site, if there is one entry in to the page that needs the javascript, on that page that will bring you to the javascript page you have the link (or form) to go to the javascript page. When the before page loads, try running some javascript to change the url or form slightly. Then if when the real page loads the url is the modified one, javascript is enabled, otherwise not.
Add the JS controls with JS. Then combine this approach with a fallback warning within <noscript>Warning</noscript>
tags.
In addition, or alternatively, Luke Smith of YUI developed a CSS solution if script is available.
I use script2style regularly on high-profile sites, it also prevents content appearing on screen before the CSS file containing 'x {display:none}'
is parsed.
If someone is interested I handled the problem this way:
<head>
<style type="text/css">
#content {display: none;}
</style>
<script type="text/javascript">
document.write('<style type="text/css">#content {display: inline;}</style>');
</script>
</head>
<body>
<noscript>Turn on JavaScript!</noscript>
<div id="content">
// some code
</div>
</body>
Thanks you everybody posting comments and answers.