View Full Version : How to test e.g. via PHP, whether browser has javascript enabled?
techwannabe
10-28-2005, 05:43 PM
Hello.
Is there a way to test e.g. via PHP, whether browser has javascript/jscript enabled for all browsers?
I'd like to create a conditional statement that says something to the effect, 'if (javascript) {<a onclick="window.open(...)"} else {<a href=... target="_blank">...}.
Is this possible or is the <noscript></noscript> tags still the way to go?
Thanks in advance.
techwannabe
10-28-2005, 08:28 PM
Hello again.
Here's an example without using the 'if' statement method from my previous post:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<a class="smallfont" href="filex.php" onClick="window.open('filex.php',myWindow','statusbar=no,me nubar=no,toolbar=no,scrollbars=yes,resizable=yes,w idth=500,height=300'); return false;">File Title</a>
<noscript><a class="smallfont" href="filex.php" target="_blank">File Title</a></noscript>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
My question now becomes, since the file location is contained within the 'href' attribute, is the '<noscript>' tag needed? Is the 'onClick' attribute just going to be ignored and the remaining portion of that <a> tag still interpreted without javascript? Is this javascript syntax interpreted the same in all browsers?
Thanks again.
[just to keep this going]
I aint done java.. I think a few others here have tho, so HANG tight.
I personally dont know how to detect if javascript is enabled via PHP. In fact, I dont think it's possible unless you can hook into the persons browser. Which is why the noscript route is the best way to go.
For reference, here are all the predefined variables:
http://us2.php.net/reserved.variables
techwannabe
11-05-2005, 07:14 PM
Thanks to you both for your reply. I have since found the solution:
The objective is to open a link in a new window REGARDLESS of whether javascript is enabled or not.
If there is a '<a href="..." onclick="window.open(...)">LINK NAME</a>' for the javascript-enabled browser AND <noscript><a href="...">LINK NAME</a></noscript> tags, the result is a duplication of the 'LINK NAME' in the non-javascript-enabled browser -- one is text and the other is a 'live' link.
Without the <noscript>...</noscript> links, the link just opens in the same (current) window, which in certain cases is just fine.
(Note: I'm using XHTML but probably doesn't matter)
In another attempt to obtain the objective stated earlier:
If you add <script...></script> tags around the '<a href="..." onclick="window.open(...)"></a>' AND <noscript>...</noscript> tags, the <noscript> tags work fine -- the <script...>...</script> tags cause a javascript error (not sure why).
Therefore,
DON'T USE <script...></script> tags around the <a href...>...</a> tags AND the <noscript>...</noscript> tags aren't needed.
SOLUTION:
========
ADD the 'target=...' attribute to the <a href="filename.php" onclick="window.open('filename.php','WIN_NAME','statusbar=n o,menubar=no,toolbar=no,scrollbars=yes,resizable=y es,width=500,height=300'); return false;" target="_blank">LINK NAME</a>
where, "WIN_NAME" is an arbitrary name of the new window that opens.
That '<a href="..." window.open(...)... target="...">LINK NAME</a>' now doubles for both javascript-enabled AND non-javascript-enabled browsers.
Hope this helps someone.
techwannabe
11-06-2005, 03:42 PM
Hello.
FYI-
I think I know the answer for this now:
If you add <script...></script> tags around the '<a href="..." onclick="window.open(...)"></a>' AND <noscript>...</noscript> tags, the <noscript> tags work fine -- the <script...>...</script> tags cause a javascript error (not sure why).It's probably because, starting at the '<a href...', this is NOT javascript syntax. In order to have this statement within <script> tags, it must conform, e.g. using the 'document.write()' function, or via another method. I used the 'SOLUTION' shown in my previous post.
helraizer
06-13-2008, 03:01 PM
I'm sure you can do it. If you have the browscap.ini installed on your server you can use the get_browser() function.
Either array, or string.
Array
$browser = get_browser(null, true);
if(in_array("[javascript] => 1", $browser) {
// their browser supports javascript.
} else {
// they don't support javascript.
}
String
$browser = get_browser(null, true);
foreach($browser as $result) {
if(stristr($result, "[javascript] => 1")) {
// They have javascript
} else {
// they don't.
}
}
Hope that helps? It works too.
Although it only tells us if their browser supports it. Not if it's disabled.
Sam
EDIT: Wow, didn't actually realise that this thread was 3 years old. o_O Sorry.
Sam, no worries. I'm sure people will find it useful.
vBulletin® v3.7.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.