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
detect if javascript is enabled
Moderator: General Moderators
-
greedyisg00d
- Forum Commoner
- Posts: 42
- Joined: Thu Feb 12, 2009 2:48 am
Re: detect if javascript is enabled
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.
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
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
give javascript to set a cookie. If it is set, read with php. If it isn't set, then java is diabled.
Good Luck
Re: detect if javascript is enabled
of course there is. a small simple ajax ( or even a standard ) javascript script can tell you if there's java on client side.JasonDFR wrote:There is no way for php to detect javascript on the user's first visit to the page.
-
sujithtomy
- Forum Commoner
- Posts: 46
- Joined: Tue Mar 24, 2009 4:43 am
Re: detect if javascript is enabled
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.............
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>
Re: detect if javascript is enabled
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.php_east wrote:of course there is. a small simple ajax ( or even a standard ) javascript script can tell you if there's java on client side.JasonDFR wrote:There is no way for php to detect javascript on the user's first visit to the page.
All you're doing is hiding the second request from the user.
Re: detect if javascript is enabled
Exactly.tasairis wrote: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.php_east wrote:of course there is. a small simple ajax ( or even a standard ) javascript script can tell you if there's java on client side.JasonDFR wrote:There is no way for php to detect javascript on the user's first visit to the page.
All you're doing is hiding the second request from the user.
Re: detect if javascript is enabled
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.
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.