Question

Is this correct? If not what is the correct syntax

I am new to php hence trying to learn.

    <?php
    // Check browser for JavaScript support

        $jsSupport='true'; ?>

        <noscript><?php $jsSupport='false'; ?></noscript>

        <?php
        if ($jsSupport == 'false') {

        include ('no-script-layout.php');

        } else {

        include ('regular-layout.php');

        }

     ?>

Or is there a better way to handle this?

Was it helpful?

Solution

<noscript> tags

You can use the noscript tags to display content to browsers with javascript disabled or redirect them to another page (a nojs-version.php for example).

<!-- Redirect to another page (for no-js support) (place it in your <head>) -->
<noscript><meta http-equiv="refresh" content="0;url=nojs-version.php"></noscript>    

<!-- Show a message -->
<noscript>You don't have javascript enabled! Please download Google Chrome!</noscript>

Modernizr

The better way to handle javascript detection (& feature) would be to use Modernizr: http://modernizr.com

Check out this SO question: What is the purpose of the HTML "no-js" class?

A basic example (without Modernizr)

You could add the class no-js on page load to your <body> tag. Then when the page loads and if javascript is enabled, you can replace the no-js with js like so:

// When the DOM is ready & loaded, do this..
$(document).ready(function(){
    // Remove the `no-js` and add the `js` (because JS is enabled (we're using it!)
    $('body').removeClass('no-js').addClass('js');

    // Assign it to a var so you don't traverse the DOM unnecessarily.
    var useJS = $('body').hasClass('js');
    if(useJS){
        // JS Enabled
    }
});

The above code is a very basic example of how modernizr works. I would highly recommend just using that.

Check out Modernizr

OTHER TIPS

To accomplish this (if you really need to know from PHP if the user has JS enabled) :

<script>
// AJAX call to your PHP script to tell it that JS is enabled
</script>

No that is not correct. All code is interpreted, and why are you using string literals instead of actual booleans? And not to mention, you're using an assignment operator instead of a comparison operator in your if statement.

It won't work, because <noscript> induces a behavior in the browser, and you are checking your condition server side.

this works very good if the JavaScript is disabled

HTML

<noscript>
         <div id="noscript-warning">This Application works best with JavaScript enabled</div>
        </noscript>   

CSS

<style>
#noscript-warning {
font-family: sans-serif;
position: fixed;
top: 0;
left: 0;
width: 100%;
z-index: 101;
text-align: center;
font-weight: bold;
font-size: 120%;
color: #fff;
background-color: #ae0000;
padding: 5px 0 5px 0;
</style>

It will not work because php is a server-side pre-processor that cannot know anything about the user's browser other than what is provided in the browser's original request, which includes nothing about its current scripting capability.

Basically, if you want to have rich content beyond the simplistic noscript tags -- they can only add non-scripted content, not hide scripted content -- you have to:

  1. Send all content -- both plain and javascript versions -- to the browser.
  2. Place all non-scripted versions of the html in div or span tags with class="nocript".
  3. Place all scripted versions of the html in div or span tags with class="script".
  4. Set css to start with div.noscript{display:block}, span.noscript{display:inline}, .script{display:none}.
  5. Have your javascript hide each element with class="noscript" with element.style.display='none', show each div with class="script" with element.style.display='block' and show each span with class="script" with element.style.display='inline'.

You also have to consider that a browser is an especially hostile environment to be programming into, as the user, or any plugins, can do anything, like disable javascript, at any time. Therefore, you have to double-check everything that the browser sends, whether by form or AJAX, in your PHP code to make sure it is complete and not corrupt.

I got it to work for me by wrapping an HTML comment tag inside the noscript tags:

<noscript><!-- </noscript>
<?php 
 $a = 42;
 echo $a;
?> 
<noscript> --> </noscript>

I had my doubts going into it...but it works, so...Let me know if there's some wierd case where it doesn't...Hope this helps with your problems :)

I think that you can do that in your script , in a index.php script , write down this code :

<?php

  //true will be returned only if javascript is not enabled 
  $JSEnabled = "<noscript>true</noscript>"; 

  //then you can do echo $JSEnabled to see the result yourself 

  //in a condition statement ...
  if($JSEnabled) {

          // ... code for JS Enabled 

  } else {

          // ... code not JS Disabled 

  }

UPDATE :

   $js = null;
   $js = (boolean) '<noscript>false</noscript>';  
   //the noscript text : false is shown when no js support detected in your browser. this value will be converted to a boolean value for testing purpose.

   if($js) {                                      
   //if $js gets the no script text , that means that the browser is not supporting the js, so this line will check if $js is set to false , the else statement is fired , otherwise the if statement is fired
    echo 'Your javascript is enabled<br>';
   } else {
    echo 'Your javascript is disabled<br>';
   }

Yo can do this with jQuery and CSS:

<body style='display: none;'>
<!--- Content goes here --->
</body>
<script>
//jQuery
$("body").css("display","block");
//Pure JavaScript
document.body.style.display="block";
</script>

how about ?

<noscript>
<?php 
//do the include thing right here 
$a=1;
?>
</noscript>
<?php
if(isset($a)){
//do nothing
}else {
//add the include u want 
};
<?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top