Show stuff only when a browser's JavaScript is enabled (using PHP)

Discussion in 'PHP' started by qwikad.com, Oct 3, 2012.

  1. #1
    I need a code that will do something like this:

    if a browser's javascript is enabled echo "this stuff" else echo "that stuff"


    Also, will this work across all browsers?


    Any help will be appreciated.
     
    qwikad.com, Oct 3, 2012 IP
  2. j_o

    j_o Well-Known Member

    Messages:
    516
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    113
    #2
    I am not sure how many ways there are to make JS and PHP to talk but i would suggest using JS to create a cookie called jsEnable. Then get your php to check if the cookie is set. That way you could do.

    
    If(isset($_COOKIE['jsEnable'])){
           // cookie set so js is enabled
    }else{
           // not set so no js
    }
    
    PHP:
    Not sure if this is the best solution but thats what comes to mind.
     
    j_o, Oct 3, 2012 IP
  3. Web Solutions

    Web Solutions Peon

    Messages:
    64
    Likes Received:
    1
    Best Answers:
    5
    Trophy Points:
    0
    #3
    Since your browser is not automatically sending such information to the web server, it is impossible for PHP to know whether you have Javascript enabled or not.

    <?php echo "<div id=\"javascript-enabled\">Disabled</div>"; ?>
    PHP:
    <script type="text/javascript">
        document.getElementById("javascript-enabled").innerHTML = "Enabled";
    </script>
    Code (markup):
    This might not be the best approach, but it doesn't require you to reload the page and will work on all browsers.
     
    Web Solutions, Oct 3, 2012 IP
  4. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,373
    Likes Received:
    1,720
    Best Answers:
    31
    Trophy Points:
    475
    #4
    Web Solutions, after I posted here I tried to google this question and they all pretty much said what you have said here.

    j_o I will try what you have suggested. Thanks for the tip.


     
    qwikad.com, Oct 3, 2012 IP
  5. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #5
    In PHP you can figure out of the browser/system combination has the javascript feature by the get_browser() method.

    If the user has javascript ability but has it disabled, you can use the noscript to send them a message to turn it back on to use your website.
     
    ThePHPMaster, Oct 3, 2012 IP
  6. RogueCZ

    RogueCZ Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi try this :

    Jquery part -
    
    $(document).ready(function(){
        $("body").removeClass("noJS");
    });
    
    Code (markup):
    CSS part -
    
    body
        .no-js-content
            :display none
    body.noJS
        .main
            :display none
        .no-js-content
            :display block
    
    Code (markup):
    HTML part -
    
    <body class="noJS">
        <div class="main">
            This will show if JavaScript is activated
        </div>
        <div class='no-js-content'>
            This will show when JavaScript is disabled
        </div>
    </body>
    Code (markup):
     
    RogueCZ, Oct 4, 2012 IP
  7. Drent123

    Drent123 Peon

    Messages:
    105
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    This used for enable we can entering in tool and make disable that.
     
    Drent123, Oct 4, 2012 IP