It seems that you're using an outdated browser. Some things may not work as they should (or don't work at all).
We suggest you upgrade newer and better browser like: Chrome, Firefox, Internet Explorer or Opera

×
I have question: how simply execute PHP code depending on JavaScript on/off state?

For example, when no using PHP, this HTML code will work everytime:
JavaScript is
<script type="text/javascript">
<!--
document.write('on');
//-->
</script><noscript>off</noscript>!
But because of how PHP works script below isn't working at all:
<script type="text/javascript">
<!--
document.write('<?php $i=1; ?>');
//-->
</script><noscript><?php $i=0; ?></noscript>
JavaScript is <?php if($i==0) echo("off"); else echo("on"); ?>!
I am looking for some simple solution to let such similar scripts work.
No posts in this topic were marked as the solution yet. If you can help, add your reply
PHP generates code
Code is sent to browser
Browser then decides whether Javascript is available.

Sorry, but PHP does not get to tell if Javascript is on.
You could theoretically make a first dummy page which does nothing but check for javascript and then redirects to an appropriate JS/non-JS php page, or calls a universal php page with a parameter or something. Otherwise it's as the monkey says, server-side pre-processing and client-side post-processing won't talk together.
avatar
Barefoot_Monkey: Sorry, but PHP does not get to tell if Javascript is on.
You could however include a simple callback which triggers a one time page reload and stores the state of JavaScript, if a reload won't trouble you and if you happen to be using sessions.

Edit:
Damn, bazilisek passed by me on the data highway.
*sends police force to fine him a GOG :p*
Post edited January 25, 2012 by Fujek
avatar
bazilisek: You could theoretically make a first dummy page which does nothing but check for javascript and then redirects to an appropriate JS/non-JS php page, or calls a universal php page with a parameter or something. Otherwise it's as the monkey says, server-side pre-processing and client-side post-processing won't talk together.
I think I've found something similar to what you are saying but at the moment I can't get it work: http://www.inspirationbit.com/php-js-detection-of-javascript-browser-settings/

I do not know if I am doing something wrong or this code is.. hmm.. somehow "obsolete"? It's dated 2007.
avatar
Lexor: I do not know if I am doing something wrong or this code is.. hmm.. somehow "obsolete"? It's dated 2007.
That looks like it ought to work. I don't see anything wrong with it at a first glance, but I'll be the first to admit I'm no expert.
avatar
Lexor: I do not know if I am doing something wrong or this code is.. hmm.. somehow "obsolete"? It's dated 2007.
avatar
bazilisek: That looks like it ought to work. I don't see anything wrong with it at a first glance, but I'll be the first to admit I'm no expert.
Execution of this
JavaScript is <?php if($nojs) echo("off"); else echo("on"); ?>!
is giving me "JavaScript is on!" everytime... hmm...
avatar
Lexor: Execution of this
JavaScript is <?php if($nojs) echo("off"); else echo("on"); ?>!
avatar
Lexor: is giving me "JavaScript is on!" everytime... hmm...
It works for me, but only the first time. That is, if I load the page with JS off, it gives me a "Javascript is off!"; then I turn JS on, refresh, get a "is on", and every subsequent refresh gives me a "is on" regardless of the actual state of JS. Hmmm.

EDIT: So it seems the browser does not reset the contents of "$_POST['jstest']" after a refresh, which I'm guessing is a feature rather than a bug. I don't see an easy way around that, but someone turning on JS while visiting your page is a fringe case you may want to ignore.
Post edited January 25, 2012 by bazilisek
avatar
bazilisek: It works for me, but only the first time. That is, if I load the page with JS off, it gives me a "Javascript is off!"; then I turn JS on, refresh, get a "is on", and every subsequent refresh gives me a "is on" regardless of the actual state of JS. Hmmm.

EDIT: So it seems the browser does not reset the contents of "$_POST['jstest']" after a refresh, which I'm guessing is a feature rather than a bug.
Ah, yes, it seems that F5 does not work in this case. But typing whole address again works. Weird.
avatar
bazilisek: I don't see an easy way around that, but someone turning on JS while visiting your page is a fringe case you may want to ignore.
Well, they will suppose do that as I was going to have "warning" message and two versions of the page for JS on/off.
Post edited January 25, 2012 by Lexor
avatar
Lexor: Well, they will suppose do that as I was going to have "warning" message and two versions of the page for JS on/off.
In that case, you'll probably be better off using sessions as Fujek suggested; $_POST is clearly unreliable. You can have finer control over sessions, even though they can be a bit of a pain in the arse.

EDIT: Alternatively, you could have two versions of the same php page; you'd put this bit of code into the non-JS one and have the hidden form there redirect to the JS version. But that's a rather dirty workaround.
Post edited January 25, 2012 by bazilisek
avatar
bazilisek: EDIT: Alternatively, you could have two versions of the same php page; you'd put this bit of code into the non-JS one and have the hidden form there redirect to the JS version. But that's a rather dirty workaround.
As I am no expert at "sessions in PHP" nor want to have duplicate pages, for now I will try to avoid such PHP/JS dependence in code of page - I will probably need to type a lot of code "two times" but it is probably simplest solution while waiting for someone with to come better one :)
Post edited January 25, 2012 by Lexor
avatar
Lexor: PHP/JS dependence
I know this might sound stupid now, so please bare with me, but what exactly are you even trying to accomplish in the end on the real application/page?
This sounds exactly like a case where there might be an easier path to accomplish what you intend to do.
avatar
Fujek: I know this might sound stupid now, so please bare with me, but what exactly are you even trying to accomplish in the end on the real application/page?
This sounds exactly like a case where there might be an easier path to accomplish what you intend to do.
First problem I was trying to do is different display of images (JS code for mouse over or displaying them somehow quite different on page if state of mouse is not possible to receive).
Workaround I will be using while typing twice:
function ShowCodeJS($code) {
echo "<script type=\"text/javascript\"><!-- document.write('";.$code."'); --></script>";
}
function ShowCodeNoJS($code) {
echo "<noscript>".$code."</noscript>";
}
Second function is just noscript code - created to complement first one.
Post edited January 25, 2012 by Lexor