Jump to content
Darkness

PHP Method To Check If JavaScript Is Enabled

Recommended Posts

Hi. Is there any known PHP method to verify whether a user has JavaScript enabled? Therefore, if it is enabled, a variable stores the value "true", and if not, stores the value "false".Otherwise, if the above is not sufficient/doesn't exist, is there a way to utilize the <noscript> tag so you can use PHP within the tags without it being executed all of the time?For example, I believe if you do this...

<noscript><?PHP//Display Messageecho "Sorry, you must have JavaScript enabled to proceed.";//Exit Pageexit;?></noscript>

The above, I believe, would process the PHP code whether JavaScript is enabled or not...Thanks.

Share this post


Link to post
Share on other sites

You would have to use AJAX to send a value to a PHP script if JS was enabled (or not at all if it wasn't). You could store this value in relation to the client's IP and on this basis execute another PHP script. As PHP is server-side it can't by itself detect whether JS is enabled.

Share this post


Link to post
Share on other sites
You would have to use AJAX to send a value to a PHP script if JS was enabled (or not at all if it wasn't). You could store this value in relation to the client's IP and on this basis execute another PHP script. As PHP is server-side it can't by itself detect whether JS is enabled.
How are you going to send an AJAX request if Javascript is disabled if there is no Javascript being ran? The AJAX script would just set a session variable to say "Javascript is enabled" you would then have to check to make sure that AJAX $_Session variable is set, and decide what to do from there.

Share this post


Link to post
Share on other sites

I was thinking of something like

Page 1 open -->	AJAX request sent from Page 1 for Script 2 -->		Script 2 Initiates session var saying "JS is enabled" -->			After reasonable time (1 sec?) Page 1 redirects to Page 3 -->				Page 3 checks whether session var initiated in Script 2 is set -->					And takes action dependent on that check.

Share this post


Link to post
Share on other sites

OK, I can't help asking... what's wrong with using JavaScript to detect if JS is enabled? I mean...

<div id="enableJS">Please enable JavaScript.<!-- Do other stuff here if JS is disabled. Note that PHP has already finished executing with or without JS being enabled. --></div><script type="text/javascript">document.getElementById('enableJS').style.display = 'none';//Do other stuff here if JS is enabled. Again note that PHP has already finished executing with or without JS being enabled.</script>

Share this post


Link to post
Share on other sites

Thats perfectly logical. By the way, the <noscript> does exactly what you just did in that example. My point was that Synook said that Javascript would have to tell the PHP(or w/e) page if javascript was enabled or not. If javascript is disabled, how is javascript going to tell the PHP page that Javascript isn't on if Javascript isn't on? You can't use an AJAX object if Javascript doesn't get parsed by the browser.

Share this post


Link to post
Share on other sites
My point was that Synook said that Javascript would have to tell the PHP(or w/e) page if javascript was enabled or not. If javascript is disabled, how is javascript going to tell the PHP page that Javascript isn't on if Javascript isn't on? You can't use an AJAX object if Javascript doesn't get parsed by the browser.
Thats the point of the system I outlined above - the variable is set if JS is on, but isn't set if JS is off. You just have to check whether it is set.

Share this post


Link to post
Share on other sites
Thats perfectly logical. By the way, the <noscript> does exactly what you just did in that example.
Yeah, but there's one reason I personally don't use noscript in favor of the above method - proxies. I've read a few times about proxies that strip JavaScript. In those cases, the browsers don't show the noscript message, because users haven't disabled JavaScript in their browsers, and at the same time, they don't execute JavaScript since it was stripped before it ever arrived to them. With the method above, people with such proxies will see the message in the div.

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...

×
×
  • Create New...