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?
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?
Share Improve this question edited Jan 2, 2013 at 12:13 Vikram Rao asked Jan 2, 2013 at 12:04 Vikram RaoVikram Rao 1,0784 gold badges25 silver badges62 bronze badges 5- 8 You should probably read more about how PHP, HTTP and HTML work... – Matti Virkkunen Commented Jan 2, 2013 at 12:06
- 1 There's no need to shout, we can hear you just fine ;-) – DaveRandom Commented Jan 2, 2013 at 12:06
-
In an IF statement, use
==
instead of=
, for a start. – Jimbo 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? – Vikram Rao Commented Jan 2, 2013 at 12:09
- OP, read Trimbitas solution to this question. It should work. – David Harris Commented Jan 2, 2013 at 12:09
10 Answers
Reset to default 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.
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 remend just using that.
Check out Modernizr
To acplish 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 parison 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:
- Send all content -- both plain and javascript versions -- to the browser.
- Place all non-scripted versions of the html in
div
orspan
tags withclass="nocript"
. - Place all scripted versions of the html in
div
orspan
tags withclass="script"
. - Set css to start with
div.noscript{display:block}
,span.noscript{display:inline}
,.script{display:none}
. - Have your javascript hide each element with
class="noscript"
withelement.style.display='none'
, show eachdiv
withclass="script"
withelement.style.display='block'
and show eachspan
withclass="script"
withelement.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 plete and not corrupt.
I got it to work for me by wrapping an HTML ment 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
};
<?