Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP check if Javascript is disabled ? 1

Status
Not open for further replies.

MrMiyagi

Technical User
Feb 11, 2009
43
FI
Hello,

I was thinking if it is possible to check if javascript is disabled with php. I am not very experienced coder but was thinking that something like this might work:

<noscript>
<div id="script_disabled" type="hidden">
</noscript>
<?php
Check if id="script_disabled" exsists

if(exsists){
javascript disabled so do something
}else{
javascript enabled so do something else}
?>

Can I do this with PHP? I am not experienced with the syntax so would be great if somebody could show how it is written properly. Thanks for all answers!


 
Set a meta refresh for 3 seconds. Specify a query string of nojs=true
Set some JavaScript to kill the meta refresh.
In php look for the presence of $_GET['nojs']. If it is present and true serve the non JavaScript content. Else JavaScript is enabled.
Store the result in a sessionvariable so that you don't have to check each refresh.
 
Hi

jpadie said:
Set some JavaScript to kill the meta refresh.
As far as I know, that is not possible. The usual way is the reverse : reload the document with JavaScript setting js=true.

Personally I consider such idea wrong. The check is made when the document is loaded, but JavaScript can be disabled anytime. ( Using [link
]QuickJava[/url] in FireFox is too easy. Sometimes I enable/disable JavaScript several times during a visit. )

Better develop a JavaScript-independent site then use JavaScript to add extra functionality.


Feherke.
 
you're right of course Feherke. i guess you could put the meta inside a noscript tag and then not bother killing it.

personally i leave js on. i would rather see a site the way the owner intended as opposed to the way i think it should look. by analogy i would not walk into a shop and start rearranging the display. if it offended me or tried to hurt me i would just leave.
 
Hi

jpadie said:
personally i leave js on.
Well, in my browser it is also on by default.

But I like to open the links to be read in separate tabs. And we can remember the times when Tek-Tips' Whitepaper scroller had no [tt]onblur[/tt] pause, so 10++ threads in separate tabs spun up the processor to 100%. Then I clicked QuickJava. And in meantime the other document's JavaScript support was also cut off.

This was just an example about a site we all know. Tek-Tips was cured, but there are many others. All I wished to say, the enabling/disabling theory sometimes is not so simple and linear.


Feherke.
 
Thanks for all answers. Could someone show an example code how exactly is this done? I could not get it working.

"Set a meta refresh for 3 seconds. Specify a query string of nojs=true
Set some JavaScript to kill the meta refresh.
In php look for the presence of $_GET['nojs']. If it is present and true serve the non JavaScript content. Else JavaScript is enabled.
Store the result in a sessionvariable so that you don't have to check each refresh."

 
feherke's solution is better. follow his route
 
What i need to do is add this line of php only if javascript is enabled:

<?php include("jscriptcontent.php"); ?>


I tried like this:

<script>
document.write("<?php include("jscriptcontent.php"); ?>");
</script>

Doesnt work...
So basically I need a way to tell PHP that javascript is enabled and make php if statement: if jscript is enabled include file if not dont include...
 
The file jscriptcontent.php adds elements to the site that i dont want to be added if javascript is disabled.

So if javascript is disabled this line of php should not be processed at all:

<?php include("jscriptcontent.php"); ?>

So I need to hide it if javascript is disabled.
 
Hi

Its output is used as parameter for [tt]document.write()[/tt] and it adds elements to the document ?

Maybe I misunderstand the situation, but personally I would re-think that. Would be possible to see jscriptcontent.php's source code ?

Feherke.
 
Im not very experienced with php and this is most likely not the best practice but jscriptcontent adds this type of thing:

<?php

$n = ( isset( $_GET['n'] ) ? $_GET['n'] : 0 );

$htmlcontent = "";

switch ($n) {

case "1":
$htmlcontent = " content for case 1 ";
break;

case "2":
$htmlcontent = " content for case 2 ";
break;

case "3":
$htmlcontent = " content for case 3 ";
break;

case "4":
$htmlcontent = " content for case 4 ";
break;

case "5":
$htmlcontent = " content for case 5 ";
break;

default:
$htmlcontent = "

content for case default

";

break;
};

echo $scriptcontent;

?>


jscriptcontent.php is also not the best name to picture the content of this file...Really need to hide this if javascript is disabled..
 
Is there a way to call php with javascript...something like this?

<script>
document.write("<?php include("jscriptcontent.php"); ?>");
</script>


Or a way to get this line of php hidden when javascript is disabled?

 
put it inside <noscript> tags.

php works on a server and javascript on the client (typically). they do not interact other than in the way that a browser interacts with a webserver.

 
Thenks for help, but I need to do the exact opposite to noscript tags. I mean that i need to run the line of php only if javascript is enabled. Any hacks for that?



Perhaps something like:

<script>var jscriptenabled = 1 </script>

if(jscriptenabled = 1)
{
<?php include("jscriptcontent.php"); ?>
}

if(jscript != 1)
{ nothing }


 
I dont think you have understood. Read my second paragraph again. Then read feherkes solutions again.

This is a two stage process. You cannot safely do this in one step.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top