Jump to content

Checking if javascript is turned on in PHP


TeddyKiller

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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.. =[

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.