3

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?

Vikram Rao
  • 1,068
  • 4
  • 24
  • 62

10 Answers10

12

<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

Community
  • 1
  • 1
Anil
  • 21,730
  • 9
  • 73
  • 100
  • You can also add this in place of the jquery method above: `(function(html){html.className = html.className.replace(/\bno-js\b/,'js')})(document.documentElement);` – Bryan Willis Dec 23 '15 at 22:05
3

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>
Sorin Trimbitas
  • 1,467
  • 18
  • 35
  • Or, instead of using AJAX at all, you could make JavaScript set a cookie to tell PHP that JavaScript is enabled. – David Harris Jan 02 '13 at 12:10
  • Yes, the JS/AJAX will make it possible to have a callback in case of further processing without page reload. But the cookie idea seems ok too. – Sorin Trimbitas Jan 02 '13 at 12:11
  • 1
    `if(getCookie('javaScriptEnabled')) { /* do other stuff here */ }` :P – David Harris Jan 02 '13 at 12:14
  • Or you could just not bother. In this day and age, nobody uses the internet without JS, because there is no internet experience without it. – DaveRandom Jan 02 '13 at 12:28
  • @DaveRandom ... think of bots or people that prefer to disable JS for increased security (noscript firefox extension) – Sorin Trimbitas Jan 02 '13 at 12:36
  • 1
    Bots shouldn't matter because your site should be designed to be navigable without client side scripts anyway - scripts are just for providing a richer UI, which a bot doesn't care about. I personally am not going to make 10 times more work for myself just to accommodate paranoid people. YMMV. – DaveRandom Jan 02 '13 at 12:41
  • DaveRandom, I never don't use NoScript, I beg to differ. – David Harris Jan 02 '13 at 12:43
  • Just gave you an example :P .. usually it is good practice to make the website work without JS and then add JS to make things easier and cooler and so on .. – Sorin Trimbitas Jan 02 '13 at 12:44
  • @DavidHarris Meh, it's your funeral :-P Seriously though, what with the proliferation of sites like FB and even our beloved SO that simply will not work (at least, in a usable way) if you turn JS off, I stopped caring about it long ago. Like I say, YMMV - but personally I have better things to do. I do use ` – DaveRandom Jan 02 '13 at 12:47
  • 1
    Make NoScript trust Facebook and SO -- all done. It may seem like NoScript is silly at first, but it really is not. I can think of many times where I've had my browser taken over by a malicious script. – David Harris Jan 02 '13 at 12:55
0

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.

David Harris
  • 2,697
  • 15
  • 27
0

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

moonwave99
  • 21,957
  • 3
  • 43
  • 64
  • It's not a matter of syntax, it's a matter of understanding how web works. Look for a nice and comprehensive resource / book, and learn from there ^^ – moonwave99 Jan 02 '13 at 12:13
0

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>
Ajit Singh
  • 1,132
  • 1
  • 13
  • 24
0

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.

Patanjali
  • 893
  • 13
  • 17
-1

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 :)

Tyler
  • 1
  • That doesn't work. The PHP **always** runs. Only the output of the PHP is handled by the noscript rules. – Quentin Feb 20 '15 at 22:02
-1

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>';
   }
Shabasha
  • 1
  • 1
-2

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>
-6

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 
};
<?
  • dude in case u didn't notice it's not about the 1 value , it's about is $a set or not , and we don't even have to set the value as 1 , its about setting it or not setting it ($a) – Fuad khattab Jan 03 '13 at 09:20
  • Ok .. then I revise my comment .. Your $a is always set :) Please learn more about js/php then try to give answers. – Sorin Trimbitas Jan 03 '13 at 09:21
  • if there was a javascript enabled that won't happen – Fuad khattab Jan 03 '13 at 09:22
  • runs when there's no javascript , plus dude it's a first answer :D – Fuad khattab Jan 03 '13 at 09:23
  • Of course you are right but only if you had the same language. Your code as it is will *always* run on "//do nothing" branch. – Sorin Trimbitas Jan 03 '13 at 09:37
  • Yes Fuad. It is because you set in PHP a variable $a, then still in PHP you try to see if it is set .. which it is (you set it at line 4). – Sorin Trimbitas Jan 03 '13 at 09:39
  • but is this php gonna run in the noscript – Fuad khattab Jan 03 '13 at 09:41
  • when javascript is enabled noscript won't run , which mean $a won't be defined , so the program asks , if the a defined that means js is not working , if it not that means js is working , is the php in noscript going to work anyway or what ??????? – Fuad khattab Jan 03 '13 at 09:43
  • 1
    Yes .. noscript means no Javascript will run inside it, PHP is an independent language. Javascript is client side, PHP is server side. – Sorin Trimbitas Jan 03 '13 at 10:43