469,270 Members | 1,164 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

How do I check if javascript is enabled in PHP?

Jim
How do I check if javascript is enabled in PHP?

TIA,

Jim
Sep 27 '06 #1
6 61133

"Jim" <Ji******@fwewef.csowrote in message
news:Cv******************@fe1.news.blueyonder.co.u k...
How do I check if javascript is enabled in PHP?

TIA,

Jim

http://www.google.com/search?q=check...&btnG=Search&h
l=en&ie=ISO-8859-1
Sep 27 '06 #2
you use a function called get_browser()
it takes no parameters and anaylzes the HTTP_USER_AGENT
it return an assoctive array
you check "javascript" key and if it is set to 1 then javascript is
enabled
For example:
$results = get_browser();
if ($results["javascript"] == 1) {
echo "You have javascript";
}

One thing you need to now is you must set the browscap.ini in your
php.ini file to the location of you browscap.ini. You can get one at
http://browsers.garykeith.com/downloads.asp

Happy detection
Jim wrote:
How do I check if javascript is enabled in PHP?

TIA,

Jim
Sep 28 '06 #3
Benjamin schreef:
you use a function called get_browser()
it takes no parameters and anaylzes the HTTP_USER_AGENT
it return an assoctive array
you check "javascript" key and if it is set to 1 then javascript is
enabled
Err: It only indicates if the browser supports javascript, not if it's
enabled or not.
JW
Sep 28 '06 #4

Benjamin wrote:
you use a function called get_browser()
it takes no parameters and anaylzes the HTTP_USER_AGENT
it return an assoctive array
you check "javascript" key and if it is set to 1 then javascript is
enabled
For example:
$results = get_browser();
if ($results["javascript"] == 1) {
echo "You have javascript";
}

One thing you need to now is you must set the browscap.ini in your
php.ini file to the location of you browscap.ini. You can get one at
http://browsers.garykeith.com/downloads.asp

Happy detection
Jim wrote:
How do I check if javascript is enabled in PHP?

TIA,

Jim
Of course that will only tell you if the browser /supports/ JavaScript
-- not whether it is actually enabled or not.

Your best bet is probably to use JavaScript to do something like set a
form variable then check if that variable got set when you process the
form.

Sep 28 '06 #5
NC
Jim wrote:
>
How do I check if javascript is enabled in PHP?
The temptation is to use get_browser() function, but it must be
resisted. get_browser() provides you with information about
what the browser CAN do. It cannot, however, tell you whether
the browser IS ALLOWED to do all it can. For example, many
browsers are capable of supporting JavaScript, but in many,
its support can be disabled.

So it's tricky. You can't do "it in one sitting"; you need to
output something (including some JavaScript), then see how the
client reacts to it. For example, this could be your index.php:

<html>
<head>
<script language="JavaScript">
window.location =
'http://www.yoursite.com/index1.php?js=1&token=<?php
echo time();
?>';
</script>
</head>
<body>
<p>Sorry, your browser does not support JavaScript...</p>
</body>
</html>

In index1.php, you check if $_GET['js'] is indeed 1 and if
$_GET['token'] is reasonably close to server's current time.

You will also need a mechanism to propagate this piece of
information...

Cheers,
NC

Sep 28 '06 #6
Jim wrote:
How do I check if javascript is enabled in PHP?

TIA,

Jim


Tim,

Just my two pennies:

Don't use PHP to check for javascript enabled / disabled.

Make your pages function properly without javascript, i.e. don't have
any inline javascript in your page - nothing essential should be done
using javascript.

Then in the pages link in an external javascript file in the header.
In this javascript file use something along the lines of:

window.onload = init;

function init() {
// add any javascript functions to the page using the DOM
}
This way, your users can use your site independently of whether
javascript is enabled or disabled as the javascript is only 'added' if
the user has javascript enabled.

An elegant and user-friendly way to deal with the problem.

Good luck,
Juliette

P.S.: if none of the above makes sense to you, stay away from javascript
in the first place.
Sep 29 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Randell D. | last post: by
13 posts views Thread by Alex Molochnikov | last post: by
7 posts views Thread by Diego | last post: by
2 posts views Thread by Mike Moore | last post: by
10 posts views Thread by David Thielen | last post: by
8 posts views Thread by Taras_96 | last post: by
6 posts views Thread by Anshul | last post: by
12 posts views Thread by Anic297 | last post: by
3 posts views Thread by =?Utf-8?B?Qkw=?= | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.