TeddyKiller Posted May 16, 2010 Share Posted May 16, 2010 I want to know how to check if javascript is turned on in the users browser. I found the function get_browser(), would this do what I want it to do.. $browser = get_browser(null, true); if($browser['javascript'] == 0) { //Javascript is not on! } Any help? Thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 16, 2010 Share Posted May 16, 2010 Use javascript to set a cookie and then use PHP to see if the cookie exists. Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted May 16, 2010 Author Share Posted May 16, 2010 good idea, but what if cookies are turned off? Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted May 16, 2010 Share Posted May 16, 2010 would this do what I want it to do.. only one way to find out .. try it! Use javascript to set a cookie and then use PHP to see if the cookie exists. that might only work on a second pass as PHP is parsed before output to the browser, meaning that PHP would check for a cookie that does not yet exist and will return false. To my knowledge, you can't tell if a user has javascript disabled/enabled with PHP. Of course there is almost always an exception to the rule. I don't know what it is you're trying to do (you should ultimately make your website(s) non-javascript compatible), but you could always have a cookie created on initial load, and have an AJAX query executed a split second later which will check for that cookie, and if found/correct, something happens (redirect, notice of some sort, etc.) It's late here, so i'm not sure if that makes sense nor if it would work. It's also not my cup of tea personally, with this being the first time i have put thought into such an event, but it's worth a try. Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted May 16, 2010 Author Share Posted May 16, 2010 but you could always have a cookie created on initial load, and have an AJAX query executed a split second later which will check for that cookie, and if found/correct, something happens (redirect, notice of some sort, etc.) I'm not sure what you mean.. The cookie would be created in javascript on the initial load... correct? I can use ajax to get an external page which would be in php to check for the cookie.. and do whatever if it doesn't exist. .. though with it being ajax.. browsers can disable it can't they? - I've seen many sites, facebook included.. where if javascript is turned off, or isn't loaded in time (Like for slow browsers.. it'll take significantly longer to load it) then it states 'Javascript is turned off' I use alot of ajax.. but a few tweaks of javascript.. but if javascript is disabled, then ajax would be wouldn't it? As they both use the javascript script tags... I think my javascript is important when I use it. I don't use it for pointless things.. Quote Link to comment Share on other sites More sharing options...
Mchl Posted May 16, 2010 Share Posted May 16, 2010 You might just as well check if client's stove is on and how much gas they have in their car's gastank. Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted May 16, 2010 Share Posted May 16, 2010 @TK - Yes JavaScript is REQUIRED for AJAX, that's what the J stands for in AJAX (Asynchronous JavaScript and XML) Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted May 16, 2010 Author Share Posted May 16, 2010 Hmm.. Well lets take facebook. What exactly would happen to some of their features? If javascript is turned on do the fancy javascript method and if javascript is turned off.. it'll go through with the PHP non-fancy version.. although php executes before javascript.. hmmm.. blast. What do facebook do.. ? There must be a way.. is it in ASP, or what.. =[ Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted May 16, 2010 Share Posted May 16, 2010 Use the noscript tag. Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted May 16, 2010 Author Share Posted May 16, 2010 I saw that.. can you explain a bit more about what I would do with it? Would <script></script> be used if javascript is enabled, and <noscript></noscript> be used if javascript is disabled? If so.. how would I do a redirect in the for noscripts. Thanks. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted May 16, 2010 Share Posted May 16, 2010 you could use the following code to provide the user a link: <noscript>You appear to have javascript turned off. <a href="http://your-site.com">Click here to continue</a> to our non-javascript site</noscript> what is it you want to do if the user has javascript turned off? bottom line is you can't tell if the user has JS turned off, you just have to provide functionality to accommodate them. Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted May 16, 2010 Author Share Posted May 16, 2010 Ah. Yes. Hmm.. Yeah.. that makes sense. Although.. the page would be loaded by the time that noscript comes up. I was looking for a more.. neater way. Like.. if javascript is disabled, to load whats in the noscript tags.. else continue to load with the page. Although. I guess I could put the noscript tag in a visible place.. Hmm.. can PHP be used in noscript tags? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted May 16, 2010 Share Posted May 16, 2010 Hmm.. can PHP be used in noscript tags? Of course it can. PHP just outputs data. It doesn't care what that data is, who is going to use it and how it is going to be used. Quote Link to comment Share on other sites More sharing options...
Mchl Posted May 16, 2010 Share Posted May 16, 2010 Although be aware, that PHP within noscript tags will be executed BEFORE the page is sent to browser (just like in any other case) Quote Link to comment 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.