AdRock Posted August 2, 2008 Share Posted August 2, 2008 I would like to check if javascript is enabled so users can still use my login form even if they have javascript disabled. Is there a way i can check and maybe do an if statement depending on the result? Quote Link to comment https://forums.phpfreaks.com/topic/117867-is-it-possible-to-check-if-javascript-is-enabled-using-php/ Share on other sites More sharing options...
Stooney Posted August 2, 2008 Share Posted August 2, 2008 php by itself can't determine if javascript is enabled. The most it can do is know of it's supported by the browser, which doesn't help. You will need to use javascript, or maybe some AJAX to see if it's enabled. (as far as I know of course) Quote Link to comment https://forums.phpfreaks.com/topic/117867-is-it-possible-to-check-if-javascript-is-enabled-using-php/#findComment-606263 Share on other sites More sharing options...
PHPTOM Posted August 2, 2008 Share Posted August 2, 2008 Beacuse it is client side scripting, it isn't possible. To do it in Javascript/HTML do this. <script type="text/javascript"> document.write('Javascript is enabled'); </script> <noscript>Javascript is disabled</noscript> Or you could do this: You have like the page you want to show on a get statement and redirect the user with javascript. If the user isn't being redirected, then JS isn't enabled else if the user is Javascript is enabled for them <script type="text/javascript"> document.location.href = "page.php?js=enabled"; </script> Quote Link to comment https://forums.phpfreaks.com/topic/117867-is-it-possible-to-check-if-javascript-is-enabled-using-php/#findComment-606265 Share on other sites More sharing options...
andyjb Posted September 24, 2008 Share Posted September 24, 2008 Not tried this yet, but an idea... On your PHP page, include the following: <body onload="jsstatus()"> <form id="jstest" action="index.php" method="post"> <?php echo '<input type="hidden" id="jstestinput"'; if (isset($_POST['jstestinput'])) { echo ' value="'.$_POST['jstestinput'].'" '; } else { echo ' value="0"'; } echo '/> ?> </form> ......... (amend the index.php to the name of the page in question) In your .js file, include the following: function jsstatus() { if (document.getElementById('jstestinput').value == 0) { document.getElementById('jstestinput').value = 1; document.getElementById('jstest').submit(); } } And back in the PHP file, add: if (isset($_POST['jstestinput'])) { $js = $_POST['jstestinput']; } else { $js = 0; } You can then use the variable $js within the page to specify actions to take if JS is enabled ($js = 1) or not ($js = 0) by using: if (isset($js) && $js == 1)) //js enabled { ... } else //js disabled { .... } When the page loads initially, if JS is not enabled, the value of the hidden field stays at 0 and the page is not submitted, hence $js does not get set. If JS is enabled, the hidden field changes from 0 to 1, and the form is submitted. When the page reloads, the hidden field retains the value submitted (i.e. 1), so the JS function does not run again, and the $js variable is created. Quote Link to comment https://forums.phpfreaks.com/topic/117867-is-it-possible-to-check-if-javascript-is-enabled-using-php/#findComment-649350 Share on other sites More sharing options...
Ashhar Posted March 19, 2009 Share Posted March 19, 2009 <noscript> <META HTTP-EQUIV="Refresh" CONTENT="0;URL=ab.php"> </noscript> Quote Link to comment https://forums.phpfreaks.com/topic/117867-is-it-possible-to-check-if-javascript-is-enabled-using-php/#findComment-788349 Share on other sites More sharing options...
kickstart Posted March 19, 2009 Share Posted March 19, 2009 Hi Can't think of a way to know in advance. However you could put a bit of javascript into the first page they visit and set a cookie with a short expiry time. Then php can just check for the existence of that cookie to know if Javascript is enabled. I have done similar things to get the screen resolution in php to resize images to fit. You could also load a non Javascript login page and use Javascript to redirect to the full login page, but this would be messy. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/117867-is-it-possible-to-check-if-javascript-is-enabled-using-php/#findComment-788359 Share on other sites More sharing options...
bambino Posted August 9, 2012 Share Posted August 9, 2012 <? if($_SESSION['JSexe']){ //3rd check js if($_COOKIE['JS']) setcookie('JS','JS',time()-1);//check on every page load else header('Location: js.html'); } //2nd so far it's been server-side scripting. Client-side scripting must be executed once to set second cookie. //Without JSexe, user with cookies and js enabled would be sent straight to js.html on first page load. elseif($_COOKIE['PHP']) $_SESSION['JSexe'] = true; else{ //1st check cookies if($_GET['cookie']) header('Location: cookies.html'); else{ setcookie('PHP','PHP'); header('Location: '.$_SERVER['REQUEST_URI'].'?cookie=1'); } } ?> <head> <script type="text/javascript">document.cookie = 'JS=JS'</script> </head> I realize this thread is quite old but I wanted to share my script and also see if there is anyone out there with other solutions. It uses PHP, javascript and cookies, and it checks whether cookies and js are enabled or not. It's quite simple really but if you need an explanation step by step you can find it here http://asdlog.com/Check_if_cookies_and_javascript_are_enabled Quote Link to comment https://forums.phpfreaks.com/topic/117867-is-it-possible-to-check-if-javascript-is-enabled-using-php/#findComment-1368175 Share on other sites More sharing options...
scootstah Posted August 9, 2012 Share Posted August 9, 2012 While interesting, that doesn't really prove that Javascript is enabled/disabled. You can have Javascript enabled but cookies disabled, and it will never work. Quote Link to comment https://forums.phpfreaks.com/topic/117867-is-it-possible-to-check-if-javascript-is-enabled-using-php/#findComment-1368186 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.