How can I tell if a user has JavaScript enabled?
Because ColdFusion is a server side technology and JavaScript is a client side technology, you will need to use a 2 page check to see if JavaScript is enabled. Your first page will perform the "is JavaScript enabled" test; your second page will display or record the results (save in a session variable etc).
One way to accomplish this task would be to set a cookie in JavaScript, and then test for the existence of this cookie via ColdFusion. If the cookie exists, then you know JavaScript is enabled.
<script language="JavaScript">
<!--
function setCookie(name, value) {
var expDate = new Date()
expDate.setTime(expDate.getTime() + 60*60*1000);
document.cookie = name + "=" + escape(value) + ";" + expDate.toGMTString();
}
setCookie('JSCookie', 'true!');
// --> </script>
<!--- Page2.cfm --->
<cfoutput>#cookie.JSCookie#</cfoutput>
Another way to test for JavaScript is to use a JavaScript redirect. If the browser supports JavaScript, it will be redirected. You could also use a HTML Meta redirect to catch all instances that ignored the JavaScript redirection, and redirect them to a set of non-JavaScript enabled pages. The code would look as follows:
<!-- Begin script
window.location.replace("hasJS.cfm");
// End script --> </script>
<html>
<head>
<META HTTP-EQUIV=REFRESH CONTENT="0;URL=noJS.cfm">
</head>
</html>
This question was written by Jeremy Petersen.
It was last updated on June 13, 2006 at 10:31:38 AM EDT.
CFML Referenced
Categories
Comments
Comment made by Critter on June 13, 2006 at 12:02 PM
Couldn't you also put the metarefresh in a <noscript> block?
Comment made by Jeremy Petersen on June 13, 2006 at 1:22 PM
Yes you can. The only drawback I have heard of on doing this is that it may not validate as xhtml strict, because <meta> is not allowed to be a child of anything but <head>.
Comment made by Damien McKenna on June 13, 2006 at 2:11 PM
What I do is haven index.cfm that has both a http refresh and a javascript location change, the JS one should happen first so if it instead goes to the http refresh page you know they don't have JS enabled.
Comment made by CMuldner on October 1, 2007 at 6:34 PM
Thanks.... Your explanation makes a lot of sense.... I think I can use it....