Jump to content

PHP Method To Check If JavaScript Is Enabled


Darkness

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.

Link to comment
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.

Link to comment
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.
Link to comment
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.

Link to comment
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>

Link to comment
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.

Link to comment
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.
Link to comment
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.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...