1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Detect JavaScript through PHP?

Discussion in 'PHP' started by Tony Brar, Nov 11, 2012.

  1. #1
    Hi guys,


    Is there any way that I could check if the user has Javascript enabled through PHP?
    If there was, I could put a PHP script before the doctype declaration, and redirect the user to something that would say "You need to have javascript enabled!"


    Thanks,
    -Tony
     
    Solved! View solution.
    Tony Brar, Nov 11, 2012 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    Hi Tony,

    In PHP you can only check if the user's browser supports Javascript, not if it is turned on or off (using get_browser) method.

    However, here is a nice trick you can use. It requires that the user go through an intermediate page (in this case index.html):

    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Loading site ...</title>
    <meta HTTP-EQUIV="refresh" CONTENT="1;URL=index.php?js=no">
    <script language="JavaScript">
    <!--
    window.location = "index.php?js=yes"
    // -->
    </script>
    </head>
    <body>
    </body>
    </html>
    
    PHP:
    The way this works is that if the user has Javascript enabled, the window.location (or any other JS method you use) will be executed. If Javascript is disabled, then the Meta Refresh tag will kick in after 1 second. You probably can do the refresh at 0 second, but 1 second should give enough time for a browser that is using lots of memory (Sorry FireFox).
     
    ThePHPMaster, Nov 11, 2012 IP
  3. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #3
    So I could set a cookie in PHP on success, remembering if the person has JS enabled...
    But what if the user enables for the test, then disables?
    There has to be a better way to do this than make the user run this page every redirect...

    -Tony
     
    Tony Brar, Nov 11, 2012 IP
  4. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #4
    You can Ajax every part of your website that you don't want them to see via Javascript - meaning that if they don't have Javascript enabled, they won't see anything because JS is disabled and the Ajax functions did not complete.

    Alternatively you can use a third party plugin like Flash or Java to check if flash is Enabled or not.
     
    ThePHPMaster, Nov 11, 2012 IP
  5. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #5
    Well, you could do that, I suppose...

    Thanks.
    -Tony
     
    Tony Brar, Nov 11, 2012 IP
  6. #6
    Just noticed on ThePHPMaster's code that the client would be redirected off the queried page (say example.com/index.php) in both instances, one in HTML (example.com/index.php?js=no) the other in Javascript (example.com/index.php?js=yes) both of which would lead to a 404 not found page. :eek:

    If you don't wish for this behaviour to occur I'd lean more towards an invisible form created in PHP and submitted with Javascript. This way if Javascript is enabled the server receives it whilst staying on page. :)


    Here's one already coded and with explanation,
    inspirationbit.com/php-js-detection-of-javascript-browser-settings/

    with minor tweaks,

    
    <?php
    if (isset($_POST['jscheck'])) {
      $nojs = FALSE;
      //JS is ON, do the PHP stuff!
      echo 'Javascript enabled';
      } else {
      // create a hidden form and submit it with javascript
      echo '<form name="jsform" id="jsform" method="post" style="display:none">';
      echo '<input name="jscheck" type="text" value="true" />';
      echo '<script language="javascript">';
      echo 'document.jsform.submit();';
      echo '</script>';
      echo '</form>';
      // the variable below would be set only if the form wasn't submitted, hence JS is disabled
      $nojs = TRUE;
    }
    if ($nojs){
      //JS is OFF, do the PHP stuff!
      echo 'Javascript disabled';
    }
    ?>
    <head>
    <script type="text/javascript">document.cookie = 'JS=JS'</script>
    </head>
    
    PHP:





    ROOFIS:cool:
     
    ROOFIS, Nov 11, 2012 IP
  7. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #7
    Ingenious JavaScript detection script.
    Just one question: Why this line of code?
    <script type="text/javascript">document.cookie = 'JS=JS'</script>
    I don't see what it does. Could you explain?

    Thanks,
    -Tony
     
    Tony Brar, Nov 13, 2012 IP
  8. ROOFIS

    ROOFIS Well-Known Member

    Messages:
    1,234
    Likes Received:
    30
    Best Answers:
    5
    Trophy Points:
    120
    #8

    whoops :eek: - just ignore it!

    I tested the snippet on my home server (I may use it myself in future) and when copy/pasting probably caught a piece of code that was below it, which was part of a java cookie write function I was coding.

    Regarding the snippet though, you may need to test on other browsers (especially IE) as commented on in the blog where this code originated from. I only tested in FF. If problems arise in IE that are not fixable in code you'd need in make a conditional statement based on browser type (see: php.net/manual/en/function.get-browser.php ) which coincidently also has provision for detecting javascript and javaapplets though not to reliably (returned null/blank with java switched on and off) as I discovered on my home server.





    ROOFIS:cool:
     
    ROOFIS, Nov 14, 2012 IP
  9. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #9
    If the script does not work with internet explorer, what would?

    Thanks,
    -Tony
     
    Tony Brar, Nov 14, 2012 IP
  10. ROOFIS

    ROOFIS Well-Known Member

    Messages:
    1,234
    Likes Received:
    30
    Best Answers:
    5
    Trophy Points:
    120
    #10
    Well did you test if it does :confused: that code was written over five years ago and probably failed on earlier versions of IE that were current at the time. It worked for me in IE9 (current release) when disabling active scripting and java applets in that browsers custom security settings.

    try this (same script but with script/noscript tags included)

    
    <script>
    document.write("JavaScript enabled!");
    </script>
    <noscript>
    JavaScript disabled!
    </noscript>
    <br>
    <br>
    <?php
    if (isset($_POST['jscheck'])) {
      $nojs = FALSE;
      //JS is ON, do the PHP stuff!
      echo 'Javascript enabled';
      } else {
      // create a hidden form and submit it with javascript
      echo '<form name="jsform" id="jsform" method="post" style="display:none">';
      echo '<input name="jscheck" type="text" value="true" />';
      echo '<script language="javascript">';
      echo 'document.jsform.submit();';
      echo '</script>';
      echo '</form>';
      // the variable below would be set only if the form wasn't submitted, hence JS is disabled
      $nojs = TRUE;
    }
    if ($nojs){
      //JS is OFF, do the PHP stuff!
      echo 'Javascript disabled';
    }
    ?>
    
    PHP:
    If both are working unanimously then the bottom line (the severside script) is obviously working too! :)
    Try this on as many browsers/mobile-browsers that are currently popular and if no problems arise (they shouldn't) then your all good to go.




    ROOFIS:cool:
     
    ROOFIS, Nov 15, 2012 IP
  11. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #11
    LOL scripting can be so easy... but it can be done better :)

    
    <?php
    // detect javascript or no javascript
    if (isset($_GET['js'])) {
        echo "Javascript " . ($_GET['js'] == 1) ? "enabled" : "not enabled";
    } else { ?>
    <script>location.href='?js=1';</script>
    <meta http-equiv="refresh" content="0; index.php?js=0">
    <?php } ?>
    
    PHP:
    at the point where you echo the code, you could set do anything.
     
    EricBruggema, Nov 16, 2012 IP
  12. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #12
    I don't understand that script, sorry.

    -Tony
     
    Tony Brar, Nov 16, 2012 IP
  13. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #13
    sorry accepted :) but now its time that you'll understand the script, start learning html, javavascript and maby a little php! :)
     
    EricBruggema, Nov 16, 2012 IP
  14. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #14
    Okay, I know HTML, I know enough PHP to get by, and little JS.
    I just don't get this part: ($_GET['js'] == 1) ? "enabled" : "not enabled"
    And I understood Roofis's code.

    -Tony
     
    Tony Brar, Nov 16, 2012 IP
  15. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #15
    The only logical way to check if JS is enabled with PHP is to use JavaScript to set a cookie. Then with PHP fetch the cookie to see if it even exists... If it doesn't exist JavaScript is either disabled or HTTP Cookies are disabled.

    In 2012 it's rare to find a browser with JavaScript and/or Cookies turned off. Most highly visited web sites depend on these technologies. Personally, I wouldn't even bother to check. If either or are turned off NO WEB SITES are fully functioning to them.
     
    NetStar, Nov 16, 2012 IP
  16. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #16
    I agree.
    I'm just gonna use JS. If the user has Javascript disabled, they at least know about it, and will be able to guess why the site doesn't work.

    -Tony
     
    Tony Brar, Nov 16, 2012 IP
  17. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #17
    Most developers don't even think twice about using JavaScript frameworks (like jquery) because they are so widely supported cross-browser and on mobile devices. Trust me...15 years ago you had to assume JavaScript may not be available on every visitor. These days, everyone has it enabled.

    It's like making a web site with 800x600 resolution in mind... 10 years ago it was the most common resolution. In 2012, most people do NOT use it..and if they do they are scrolling left to right on EVERY site they visit.

    Don't kill yourself. Your web site won't be accessible by 100% of the community. Just worry about the 98%. Don't degrade your site to support people with ancient or messed up browsers and computers....the majority of your visitors will suffer. Support the Majority...not the minority...
     
    NetStar, Nov 16, 2012 IP
  18. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #18
    I agree.

    -Tony
     
    Tony Brar, Nov 16, 2012 IP
  19. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #19
    @NetStar; i don't agree that the only way to verify is a cookie!!! lol

    btw to explain you the code

    print to screen IS ?js IS 1 YES? "enabled" NO? "disabled"

    lol
     
    EricBruggema, Nov 16, 2012 IP
  20. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #20
    You did not read my post then. I said the most LOGICAL way is to set a cookie with JavaScript. Why? Because JavaScript is processed client side whereas PHP is processed server side. The same HTTP Cookie can be fetched with either method thus making it the most logical way to check if a client side is functioning with a server side method.
     
    NetStar, Nov 17, 2012 IP