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?

5
  • 8
    You should probably read more about how PHP, HTTP and HTML work... Commented Jan 2, 2013 at 12:06
  • 1
    There's no need to shout, we can hear you just fine ;-) Commented Jan 2, 2013 at 12:06
  • In an IF statement, use == instead of =, for a start. Commented Jan 2, 2013 at 12:08
  • @dave Sorry for that I am new to this place hence learning things slowly. By the way do you have a solution to my question? Commented Jan 2, 2013 at 12:09
  • OP, read Trimbitas solution to this question. It should work. Commented Jan 2, 2013 at 12:09

10 Answers 10

13

<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

Sign up to request clarification or add additional context in comments.

1 Comment

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

10 Comments

Or, instead of using AJAX at all, you could make JavaScript set a cookie to tell PHP that JavaScript is enabled.
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.
if(getCookie('javaScriptEnabled')) { /* do other stuff here */ } :P
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.
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.
|
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.

Comments

0

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

1 Comment

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

Comments

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.

Comments

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

1 Comment

That doesn't work. The PHP always runs. Only the output of the PHP is handled by the noscript rules.
-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>';
   }

Comments

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

Comments

-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 
};
<?

9 Comments

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)
Ok .. then I revise my comment .. Your $a is always set :) Please learn more about js/php then try to give answers.
if there was a javascript enabled that won't happen
<noscript> runs when there's no javascript , plus dude it's a first answer :D
Yes .. noscript means no Javascript will run inside it, PHP is an independent language. Javascript is client side, PHP is server side.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.