Detect javascript

Discussion in 'PHP' started by sg552, Sep 22, 2008.

  1. #1
    Hi,

    Anyone know the code to check for visitor browser javascript is turn on or off?
    Then if the javascript is off my web content will be hide and if javascript enable my visitor can view my website content.

    This will run in PHP. Any idea for the code:confused:

    Thanks in advance.
    :)
     
    sg552, Sep 22, 2008 IP
  2. lui2603

    lui2603 Peon

    Messages:
    729
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <noscript></noscript>, maybe?
     
    lui2603, Sep 22, 2008 IP
  3. Sillysoft

    Sillysoft Active Member

    Messages:
    177
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #3
    You cant really check if javascript is on from php because php is server side script and javascript is client side. However thinking about it could you create a cookie with javascript and check for the value with php to see if javascript is enabled? The cookie wouldnt be created if it was turned off right? Just a thought.
     
    Sillysoft, Sep 22, 2008 IP
  4. mehdi

    mehdi Peon

    Messages:
    258
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Paste below code, in your HTML Page anywhere you want.
    
    <script>
    if (true) {
      document.write("JavaScript is turned on!");
    }else{
      document.write("JavaScript is turned OFF,please enable Javascript!");
    }
    </script>
    
    Code (markup):
     
    mehdi, Sep 22, 2008 IP
  5. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #5
    er. and if js is disabled, what exactly will to the evaluation and the document write for you?

    the cookie idea is the smart one, thanks!
     
    dimitar christoff, Sep 22, 2008 IP
  6. Danny

    Danny Active Member

    Messages:
    732
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    78
    #6
    Lol @ Mehdi, you were kidding right?

    I am wondering if you could try to set something using JSON.

    Alternatively the way I have done this in the past is to either use javascript to fill in a hidden form field or set a value in the cookie, I then have PHP check the value of the cookie or form field.

    From there it is a simple if statement to check the value, if it is filled in then we have Javascript access, if we don't then well obviously it is not enabled.
     
    Danny, Sep 22, 2008 IP
  7. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #7
    I would use Ajax.

    When the user comes in, let Ajax implement a cookie then refresh the page.

    Unless that cookie exist, do not show the content (which means JS is disabled).

    Peace,
     
    Barti1987, Sep 22, 2008 IP
  8. ilook

    ilook Well-Known Member

    Messages:
    1,602
    Likes Received:
    15
    Best Answers:
    1
    Trophy Points:
    165
    #8

    show the script ;)
     
    ilook, Sep 22, 2008 IP
  9. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #9
    <?
    if($_COOKIE['js_enabled'] != "1") {
    ?><html>
    <head>
    <script type="text/javascript">
    function setCookie(c_name,value,expiredays) {
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+expiredays);
        document.cookie=c_name+ "=" +value+
          ((expiredays==null) ? "" : ";path=/;expires="+exdate.toGMTString());
    }
    setCookie("js_enabled", "1", 1);
    document.location = document.location;
    </script>
    </head>
    <body>
    <noscript>You must have JavaScript enabled to view this website!</noscript>
    </body>
    </html><?
    } else {
    ?>SHOW ALL YOUR SITE'S ACTUAL CONTENT HERE SINCE JAVASCRIPT IS ENABLED<?
    }
    ?>
    Code (markup):
    Should do the trick.. it first checks to see if the js_enabled cookie is set using PHP and if it's not, it puts out a small HTML page that attempts to create the cookie with JavaScript and if JS is actually on it'll reload the page and then the PHP will see the cookie and will print out the other part of the page instead. If JS is not enabled then it'll show them what's in the noscript tags.

    The js_enabled cookie, how it's setup now, will only last 1 day which means it'll check daily if they have javascript enabled or not.
     
    zerxer, Sep 22, 2008 IP
  10. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #10
    having a refresh is not ideal - what if he has form data in $_POST? it will be gone...
     
    dimitar christoff, Sep 23, 2008 IP
  11. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #11
    If they somehow got to a form already and submitted it then it most likely already set the required cookie and won't have to go through that again for that period.
     
    zerxer, Sep 23, 2008 IP
  12. Danny

    Danny Active Member

    Messages:
    732
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    78
    #12
    Most likely????

    Never a good term to use!

    Refresh is the wrong choice here. Its a fairly simple concept and idea
     
    Danny, Sep 26, 2008 IP
  13. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Okay then how do you recommend doing EXACTLY what he wants? You said to set a hidden value or whatever in the form but if you read what he wants, he doesn't want his site to display at all if they don't have JavaScript on so there's no form there for them anyways until it verifies that it's enabled. If the page is blank while it checks for JavaScript to set the cookie, how would you go about making it reload the page for PHP to see the set cookie and output the actual page data if not by refreshing it?

    If he's truly going to be using POST data which he did not say he was, then what he could do is have PHP output all the POST data onto the page in hidden fields and instead of having JavaScript refresh the page, have it submit the hidden form.
     
    zerxer, Sep 26, 2008 IP