Is this correct? If not what is the correct syntax
I am new to php hence trying to learn.
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:
div
or span
tags with class="nocript"
.div
or span
tags with class="script"
.div.noscript{display:block}
, span.noscript{display:inline}
, .script{display:none}
.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 :)
<noscript>
tagsYou 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>
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?
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
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>
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>