detect if javascript is enabled

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
greedyisg00d
Forum Commoner
Posts: 42
Joined: Thu Feb 12, 2009 2:48 am

detect if javascript is enabled

Post by greedyisg00d »

Is it possible to detect if javascript is enabled in a browser using php? What I want is when javascript is disabled it will prompt the user that they must enable they javascript.

Any sample codes is much appreciated. Thanks
JasonDFR
Forum Commoner
Posts: 40
Joined: Wed Jan 07, 2009 3:51 am

Re: detect if javascript is enabled

Post by JasonDFR »

There is no way for php to detect javascript on the user's first visit to the page.

After the first page load, one way is to use javascript to set a cookie, then on subsequent page loads, test for the existence of this cookie. Note that this solution requires that the user has cookies enabled.

But for your situation, displaying a message to users who DO NOT have javascript enabled, you could put a message into your page using HTML, a div that contains whatever message you wish to display to users who do not have javascript enabled. When the page is loaded, immediately use javascript to remove this DIV. If javascript is enabled, the user will not see the message.

I'm interested to hear the pros and cons of this solution as well as others.
sujithtomy
Forum Commoner
Posts: 46
Joined: Tue Mar 24, 2009 4:43 am

Re: detect if javascript is enabled

Post by sujithtomy »

Hello,

give javascript to set a cookie. If it is set, read with php. If it isn't set, then java is diabled.

Good Luck :)
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: detect if javascript is enabled

Post by php_east »

JasonDFR wrote:There is no way for php to detect javascript on the user's first visit to the page.
of course there is. a small simple ajax ( or even a standard ) javascript script can tell you if there's java on client side.
sujithtomy
Forum Commoner
Posts: 46
Joined: Tue Mar 24, 2009 4:43 am

Re: detect if javascript is enabled

Post by sujithtomy »

Hello,

Add this code between the <head> tags in your web page

<noscript>
<meta http-equiv="refresh" content="0; url=javascript_disabled.html" >
</noscript>

If javascript is disabled, user will redirected to 'javascript_disabled.html'. on that page you can display a message something like

JavaScript is disabled in your browser, you must enable it in order to continue.

also provide some information on how to enable javascript in IE, FF etc.............

Code: Select all

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Title</title>    
    <noscript>
        <meta http-equiv="refresh" content="0; url=javascript_disabled.html" >
    </noscript>    
</head> 
<body> 
    <p>Lorem Ipsum.................</p> 
</body>
</html>
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: detect if javascript is enabled

Post by requinix »

php_east wrote:
JasonDFR wrote:There is no way for php to detect javascript on the user's first visit to the page.
of course there is. a small simple ajax ( or even a standard ) javascript script can tell you if there's java on client side.
Technically speaking, PHP will get that request as the second page load. First time is when PHP has to print out the HTML page with the JavaScript, second time is when PHP handles the AJAX request.
All you're doing is hiding the second request from the user.
JasonDFR
Forum Commoner
Posts: 40
Joined: Wed Jan 07, 2009 3:51 am

Re: detect if javascript is enabled

Post by JasonDFR »

tasairis wrote:
php_east wrote:
JasonDFR wrote:There is no way for php to detect javascript on the user's first visit to the page.
of course there is. a small simple ajax ( or even a standard ) javascript script can tell you if there's java on client side.
Technically speaking, PHP will get that request as the second page load. First time is when PHP has to print out the HTML page with the JavaScript, second time is when PHP handles the AJAX request.
All you're doing is hiding the second request from the user.
Exactly.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: detect if javascript is enabled

Post by php_east »

i understand what you mean and agree in part.

however, ~JasonDFR said on first visit, not on first page get. As far as user is concerned he is on his first visit, and as far as server is concerned it may be 1~n number of gets to load the first page i.e. for ajax driven contents, for the users first visit.

techically, you are both absolutely correct of course.

p/s also this test i think has to be made on all pages, and not use a cookie, because the user may decide to switch on his javascript in between pages. in fact i think all browsers should have this as part of their header. makes life easier for a lot of people.
Post Reply