Hi,

I got a code that took a long time to execute. I only want to execute it when the browser does not have javascript enabled.

Is there a way to know whether javascript is enabled or not in PhP?

    Write a Javascript application that sets a variable in a form. When the form is posted, that variable will only exist if JS is available.

      So, basically, it's a work around. There is no PHP ways to do this isn't it?

        Can PHP gets some info about the users? IP, Javascript enabled, etc.?

          Only if the client sends that information to the server.

            teguh123:

            Please remember that PHP runs server side and Javascript runs client side. They are on separate computers.

            In fact, PHP is designed so that the client side can be just about anything. Seriously, it can be anything.

            1. Any platform (Linux, Mac, Windows, BeOS, Amiga, Motorola cell phone, etc)
            2. Any browser (FF, IE, Netscape, Mosaic, Your Favorite Email Client, etc)
            3. Any medium (computer, paper, video screen, JPG file, email, iPod Shuffle, etc)

            Historically, it's worked like this: PHP creates something and then the web server (Apache) delivers it to the other end. What the other end does with the HTML / JPG / PDF / MP3 is their business. In that way, it's been much like a broadcast medium: ie. The TV station broadcasts the signal; Whether you have a color TV or a b/w TV is your business.

            With Ajax and web forms, you can write something for the client side that will gather up some information about the client side and report it back to the server.

            But let's say you're using PHP to create a PDF that the user will print on their laser printer. It would take a special kind of magic for PHP to figure out whether the user is using a laser printer or an inkjet. Or even more difficult, white paper versus blue stationery. Maybe that example is too esoteric but it should show that PHP is designed for more than just web sites... and isn't designed to automagically know what the recipient's environment looks like.

            Unless, as WP said, the client sends that information to the server. So using Javascript to set a variable and report it to the server isn't a work around. It's how you do it.

              Weedpacket wrote:

              Only if the client sends that information to the server.

              I know that client sends many information to the server, such as IP. Does the client also send information whether javascript is enabled or not?

                teguh123 wrote:

                I know that client sends many information to the server, such as IP. Does the client also send information whether javascript is enabled or not?

                NO!!!

                You've been told 3 times now!

                  Only the first time explicitly.

                  Anyway, I have figured out a work around. I make a javacript redirect at the beginning and make my server wait for 7 seconds. If the site is not redirected within 7 seconds than the PHP will execute.

                  Hence, PHP will only execute when there is not javascript installed, just like what I wanted.

                  Is there a better way than this?

                    Is one time not enough to make you understand the answer?

                    And as already stated, it is NOT a workaround. It is how things are done.

                    Yes, there is a better way. It have already been mentioned in this thread. Or maybe you need it said 3 additional times like you did with your first question?

                      Your way will not work properly.

                      What if someone has really slow internet and it takes more than 7 seconds to load a page?

                        Allright, so far I got an advice on how to run a PhP program when Javascript is installed. Namely setting up a variable in javascript and call a form.

                        What I want is to run a PhP program when javascript is NOT installed.

                        I don't think that's possible isn't it?

                          That's a good question. It depends on how and when the PHP script is being called.

                          You can do it anytime that you know that the PHP script is being called from a link on your page. You can make it work everytime if you are willing to play some Javascript tricks.

                          Consider two ways of accessing a page on your site:

                          1. user types in a url in their web browser.
                          2. user clicks a link on your page

                          In situation 1, we have already established that the browser does not announce to the PHP script whether or not JS is active so no, the PHP would not know whether or not to execute.

                          In situation 2, you can include a variable in the link maybe "javascript=no". Then you can use Javascript to change the value of that variable to "javascript=yes" and the PHP script would know whether or not to execute.

                          But then if you wanted to play some games with Javascript, you could probably make situation 1 work if you put a meta refresh in the page that forced a page to redirect to another page essentially forcing the page to report back to the server. So hypothetically, if the user types http://www.yourdomain.com into their browser, you could have an index.html page that displays, Javascript code that sets a variable, and then a meta refresh that forces the page to redirect to another page (maybe index2.php). Then, index2.php should be able to check for the presence of that variable and know whether or not JS is present in the browser.

                          I've never tried that but it should work as long as you can write some Javascript that is able to modify the URL in the metarefresh before the metarefresh happens.

                          If that works, you might find that the timing (or order of operations) might vary from browser to browser which would cause a situation where sometimes you are able to tell that JS is present and sometimes the PHP gets the wrong information.

                          But all of that applies to situation 1 only.

                          In situation 2, the timing and the order of operations are irrelevent so you will always get a correct reading of when JS is present.

                          So now you need to determine whether or not this function can be limited to just pages where the user is clicking a link on your page.

                          What I would do is this:
                          write a header that gets included on every page
                          in the header, check for the presence of a cookie called "JS_enabled"
                          if no cookie, then make a form, use JS to set a variable in the form
                          tell the user that they must click the button to continue
                          in the header, check for the presence of that form's message
                          if it's there, set a cookie called JS_enabled
                          then have the page perform as desired (that is, the PHP executes if no JS present)

                          That way, you are forcing the user to do an extra step the first time they are at the site but then you have a clean way of knowing whether or not they have JS after that. Of course, this would make your web site useless to anyone without cookies turned on. 🙂 (but who needs those people anyway!)

                            Sounds like a good plan.

                            Sorry to offend you guys. I am basically testing this code:

                            <code>

                            <?
                            echo 'hi world';
                            ?>
                            <noscript>
                            <?
                            echo 'hello world';
                            ?>
                            </noscript>

                            </code>

                            What I see if I access the site with javascript enabled browser is
                            hi world

                            One way to interpret the result is that PhP doesn't know that the browser support javascript or not. So the echo 'hello world' is still executed anyway. However, because it shows up on <noscript>, the client choose to hide it.

                            So we need further experiment.

                              I put the following code:

                              <?
                              echo 'hi world';
                              ?>
                              <noscript>
                              <?
                              echo 'hello world';
                              $hello='hello';
                              ?>
                              </noscript>

                              <?
                              echo $hello;
                              ?>

                              It seems that the part in <noscript> is STILL executed. It's just that the client choose not to display it just like what you guys said 🙁

                              What I got is:

                              hi world hello

                              Let's try your plan 😃

                                The sample script you just wrote doesn't test what you said you were trying to do.

                                You said that you wanted the PHP to execute (on the server side) only if JS is not active (on the client side).

                                So let's say that we find some way for the browser to tell the server that JS is or isn't active. Once we do that, then we can write PHP like this:

                                <?
                                if ($javascript_is_not_active) { print "hello world"; }
                                else { print "hi world"; }
                                ?>

                                My example uses the "hi/hello" indicator that you used in your test. Since your goal was to make PHP NOT execute when Javascript is present, this example might be more clear:

                                <?
                                if ($javascript_is_not_present) {

                                print "php is doing stuff";
                                $x=12;
                                $y=13;
                                $z = $x + $y;
                                print "PHP is doing all sorts of stuff because JS is NOT present";
                                }
                                else {
                                // do nothing because JS is present
                                }
                                ?>

                                  Write a Reply...