Hey Everyone, I have a php page that I created a login for it uses a jquery drop down to show the form that displays the login and I am trying to fix it to work also if js is disabled. Right now it is set so that if java script is enabled and you click on a link (href="#") then the box drops down. However if js is disbaled i need that link to instead show (href="login.php"). My thought was to check if javascript is enabled by setting a cookie and then php will just check if that cookie is created. So i go and set my cookie using: <SCRIPT LANGUAGE="JavaScript"> var dt = new Date(), expiryTime = dt.setTime( dt.getTime() + 1800000 ); document.cookie = 'JsCheck=' + 'Javascript Check' + ';expires=' + dt.toGMTString(); </SCRIPT> Code (markup): Then right under that I do: <?php if(isset($_COOKIE['JsCheck'])){ die('JS Enabled'); //The die is just here for testing purposes } ?> PHP: First I make sure that the cookie is not set and then with js enabled on my browser I go to my page. Once loaded I check my cookies and firefox reports back to me that my cookie was successfully created. However my php check did not trigger and kill the loading of the page. Once that is done though if I go and reload the page with the cookie already set php picks up the cookie and kills the refresh. Is there a reason why this check does not work upon the initial loading when the cookie is set? Thanks in advance, Joe
Check to see if there browser supports JS: PHP: $results = get_browser(); if ($results["javascript"] == 1) { echo "You have javascript"; } Code (markup): 99% will be enabled. But if it is important to know if it is enabled then on load have your JS change the link using Jquery. by default have the link point where it would go without javascript. $("a[href^='http://google.com']") .each(function() { this.href = this.href.replace(/^http:\/\/javascript\.google\.com/, "http://google.com"); }); Code (markup):
That was the point. If it ran JS was enabled otherwise JS was disabled. But HowDoYou's suggestion of using get_browser() is much better. Thanks for the help.
I went to try get_browser however browscap is not set in my php.ini (which for some reason my hosting company wont let me change) so that option is out. How would I go about replacing the link as you said. the two links would be <a id="open" class="open" href="login.php">Log In</a> <!--JS disabled --> <a id="open" class="open" href="#">Log In</a> <!--JS disabled --> Code (markup):
as for my experience, get_browser() does not work everytime depending on the server. but if it works for you, its fine..
Detecting and accounting for no Javascript is trivial: <script> alert('js is on'); </script> <noscript> <a href="http://www.mysite.com">noscript link</a> </noscript> Code (markup): If Javascript is on you get the alert, if not you get the link. It works in every modern browser.