Hi, Anyone know the code to check for visitor browser javascript is turn on or off? Then if the javascript is off my web content will be hide and if javascript enable my visitor can view my website content. This will run in PHP. Any idea for the code Thanks in advance.
You cant really check if javascript is on from php because php is server side script and javascript is client side. However thinking about it could you create a cookie with javascript and check for the value with php to see if javascript is enabled? The cookie wouldnt be created if it was turned off right? Just a thought.
Paste below code, in your HTML Page anywhere you want. <script> if (true) { document.write("JavaScript is turned on!"); }else{ document.write("JavaScript is turned OFF,please enable Javascript!"); } </script> Code (markup):
er. and if js is disabled, what exactly will to the evaluation and the document write for you? the cookie idea is the smart one, thanks!
Lol @ Mehdi, you were kidding right? I am wondering if you could try to set something using JSON. Alternatively the way I have done this in the past is to either use javascript to fill in a hidden form field or set a value in the cookie, I then have PHP check the value of the cookie or form field. From there it is a simple if statement to check the value, if it is filled in then we have Javascript access, if we don't then well obviously it is not enabled.
I would use Ajax. When the user comes in, let Ajax implement a cookie then refresh the page. Unless that cookie exist, do not show the content (which means JS is disabled). Peace,
<? if($_COOKIE['js_enabled'] != "1") { ?><html> <head> <script type="text/javascript"> function setCookie(c_name,value,expiredays) { var exdate=new Date(); exdate.setDate(exdate.getDate()+expiredays); document.cookie=c_name+ "=" +value+ ((expiredays==null) ? "" : ";path=/;expires="+exdate.toGMTString()); } setCookie("js_enabled", "1", 1); document.location = document.location; </script> </head> <body> <noscript>You must have JavaScript enabled to view this website!</noscript> </body> </html><? } else { ?>SHOW ALL YOUR SITE'S ACTUAL CONTENT HERE SINCE JAVASCRIPT IS ENABLED<? } ?> Code (markup): Should do the trick.. it first checks to see if the js_enabled cookie is set using PHP and if it's not, it puts out a small HTML page that attempts to create the cookie with JavaScript and if JS is actually on it'll reload the page and then the PHP will see the cookie and will print out the other part of the page instead. If JS is not enabled then it'll show them what's in the noscript tags. The js_enabled cookie, how it's setup now, will only last 1 day which means it'll check daily if they have javascript enabled or not.
If they somehow got to a form already and submitted it then it most likely already set the required cookie and won't have to go through that again for that period.
Most likely???? Never a good term to use! Refresh is the wrong choice here. Its a fairly simple concept and idea
Okay then how do you recommend doing EXACTLY what he wants? You said to set a hidden value or whatever in the form but if you read what he wants, he doesn't want his site to display at all if they don't have JavaScript on so there's no form there for them anyways until it verifies that it's enabled. If the page is blank while it checks for JavaScript to set the cookie, how would you go about making it reload the page for PHP to see the set cookie and output the actual page data if not by refreshing it? If he's truly going to be using POST data which he did not say he was, then what he could do is have PHP output all the POST data onto the page in hidden fields and instead of having JavaScript refresh the page, have it submit the hidden form.