Javascript detect

Hi everyone

How to see if javascript is enabled while loading a web page without looking at Tools/Internet Options etc...
Do we have to create 1 html file showing if it is enable and then redirect to another web page telling the user what to do if javascript is disabled?

Do we put this in <body> of javacheck.html

<SCRIPT LANGUAGE="JavaScript">
if(navigator.javaEnabled()) {
   document.location.href = "javaenabled.htm";

then on javaenabled.html

put in the body
<p>java enabled

what if its not enabled?

Is it the same way for detecting cookie and enabling them if required?

Thanks
ThiboAsked:
Who is Participating?
 
GitchoCommented:
Not sure what to tell you with the browser issue, but the bottom line is that if you're seeing "If this text is displayed, your browser supports scripting and has JavaScript enabled!" then javascript hasn't obviously been disabled.   You many want to post that one in the OS -> Windows XP forum ...

Couldn't you just do the opposite, and use javascript to redirect users who HAVE jajvascript enabled ?

<script language="javascript">
      window.location = "newpage.html";
</script>

If you stick that code in your page, then you know that anyone who visits newpage.html HAS javascript enabled.  Those who don't will see that page.
0
 
MoonCalfCommented:
Hi,

This is such a stupidly simple suggestion, but I believe it will work.

In your main page (a non-javascript version of your javascript page, or maybe a page that says "You need JavaScript blah blah blah etc.."), place the following script...

<script language="JavaScript">
  location.replace("javaenabled.htm")
</script>


That's it.  If JavaScript is not enabled/unavailable then the redirect won't work.

Hope this helps.

MoonCalf.
0
 
MoonCalfCommented:
Sorry - forgot the cookie bit.  Your best bet for detecting most technologies is simply to try using them.  Ensure you have a fallback position if the technology does not exist - in other words, never assume something will work - always think about the users that cannot access what you are offering and try to offer something at least similar.  This is real basic old-skool style thinking, like <frames> and <noframes>, which reminds me..... there is also the <noscript> tag which could come in useful for you too.

Good luck,

MoonCalf.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
ThiboAuthor Commented:
Hi MoonCalf

I tried that earlier on on IE 6.0 but didnt work...I disabled javascript with tools options...but it was still redirecting me to javascript is enabled...

It works with mozilla firefox though...but the main web browser used in this context is IE

thanks
0
 
cgarveyCommented:
You need to close all IE instances for some settings to kick in.
I be if you disable javascript, then close all IE windows, then open one upand try again it will work or rather, won't work :)

As far as cookie detection goes, there's a useful article here that should help

 http://codepunk.hardwar.org.uk/ajs34.htm
0
 
ThiboAuthor Commented:
Thanks for the URL cgarvey

However I did close all my IE windows (xept for 1 where I disabled javascript from)
and when i opened the javacheck.html it still goes to javaenabled.htm

In javacheck.html where I have in <body>java is disabled...
I put MoonCalfs code:

<script language="JavaScript">
  location.replace("javaenabled.htm")
</script>

I have no idea y it is still not working...






0
 
StormyWatersCommented:
You need to close the one where you disabled javascript from as well.
0
 
GitchoCommented:
The NOSCRIPT area is used to display a message or other contents when the visitor is using a non-JavaScript capable browser. The visitors with JavaScript capable browsers will ignore this area and perform the JavaScript coding.

Example :

<html>
<head>

<script type="text/javascript">
<!--
alert ("This is a JavaScript ALERT box.")
// -->
</script>

<noscript>
If you see this message, your browser is not running JavaScript.
</noscript>

</head>
<body>
Main page contents here.
</body>
</html>

The JavaScript capable browser will display a pop-up alert box and ignore the NOSCRIPT area. The non-JavaScript browser will ignore the actual JavaScript area and display the contents within the NOSCRIPT area.
0
 
GitchoCommented:
It's not possible to redirect a page (using javascript, or any server-side language) to a different page if the client doesn't have javascript enabled.

The clients browser does not include any information in the headers about whether javascript is enabled or not when the client reqeusts a page.

Browsers can only read the code above and display alternate info in the <noscript> tags ...  
0
 
ThiboAuthor Commented:
Hi Gitcho

It is still showing me javascript is enabled

I tried :

<HTML>
<head>
<script type="text/javascript">
<!--
alert ("This is a JavaScript ALERT box.")
// -->
</script>
<noscript>
If you see this message, your browser is not running JavaScript.
</noscript>
</head>
<BODY>
<b>javascript is enabled!!!
</BODY></HTML>

and looked up at this website:

http://www.webaim.org/techniques/javascript/#disabling

Any hints?
0
 
ThiboAuthor Commented:
I simply put

<html>
<head>
<title>Testing for JavaScript</title>
</head>
<body>
<h1>Determine if JavaScript is Enabled</h1>
<script type="text/javascript">
<!--
document.write("If this text is displayed, your browser supports scripting and has JavaScript enabled!")
//-->
</script>
<noscript>JavaScript is NOT enabled!</noscript>
<hr />
</body>
</html>

the result: If this text is displayed, your browser supports scripting and has JavaScript enabled!
0
 
GitchoCommented:
i'll check the code locally ... 1 sec ...
0
 
GitchoCommented:
OK ... works on my end ...

If you're using Internet explorer, follow the instructions on http://www.webaim.org/techniques/javascript/#disabling , and set your active scripting to disabled.

When I do that, and to go a page with the code above, i get
------------------------------------------------------------------------

Determine if JavaScript is Enabled
JavaScript is NOT enabled!

-----------------------------------------------------------------------

There's nothing wrong with the code.  It's likely your local settings.
0
 
ThiboAuthor Commented:
I agree with you Gitcho, it works when I directly load it from the website...
------------------------------------------------------------------------

Determine if JavaScript is Enabled
JavaScript is NOT enabled!

-----------------------------------------------------------------------

after i set active scripting to disabled.
But once i create a new html file with the code above into my directory it shows javascript is enabled

Did you create a new html file just now or you tested it directly from the website?
If you created a new html file and it works then Im all wrong and I made a mistake somewhere
0
 
GitchoCommented:
I think you might be misunderstanding what's happening.  Whether or not javascript works is determined by your browser.

Here's the process.  
- you type a url into your browser and hit enter (eg. www.google.com)
- the browser makes a trip to googles web-servers, asking for the page from www.google.com
- google's web-severs grab the file from it's local servers, and send the resulting code back to your browswer (lets assume the page google is sending contains both HTML and javascript)
- when your browser recieves this code, it "translates" what it receives, and displayes it in your browser.  
- during the translation, it reads the javascript on the page, and executes the code it locally on your machine (that's why javascript is a client-side scripting language - the code is processed on the client)
- if your browser is set to DISABLE javascript, then you've told your browser  NOT to take any action on the javascript it sees - it will completely ignore any javascript it sees - just like an HTML comment.  It will then look for the code contained in the <noscript> tags and execute that.  

There is nothing that google can program into their page to prevent you from using javascript, or determine IF you have javascript enabled.  Your browser doesn't tell google's web-server it's script settings when it requests the page.

0
 
davidlars99Commented:
this is how it's done in ASP

<%
set oBrowser = Server.CreateObject("MSWC.BrowserType")


respons.write(oBrowser.Tables)

respons.write(oBrowser.Frames)

respons.write(oBrowser.JavaApplets)
 
respons.write(oBrowser.Cookies)

respons.write(oBrowser.ActiveXControls)

respons.write(oBrowser.BackgroundSounds)

respons.write(oBrowser.JavaScript)

respons.write(oBrowser.VBScript)

set oBrowser = Nothing

%>

and this is all you can do and can't do with your issue
http://webreference.com/tools/browser/javascript.html

cheers!
 dave
0
 
ThiboAuthor Commented:
Thanks Gitcho for the reply. You say in this sentence:
>>- if your browser is set to DISABLE javascript, then you've told your browser  NOT to take any action on the >>javascript it sees - it will completely ignore any javascript it sees - just like an HTML comment.  It will then look >>for the code contained in the <noscript> tags and execute that.

but if I run this code into my browser with javascript disabled:

<html>
<head>
<title>Testing for JavaScript</title>
</head>
<body>
<h1>Determine if JavaScript is Enabled</h1>
<script type="text/javascript">
<!--
document.write("If this text is displayed, your browser supports scripting and has JavaScript enabled!")
//-->
</script>
<noscript>JavaScript is NOT enabled!</noscript>
<hr />
</body>
</html>

it still shows me "If this text is displayed, your browser supports scripting and has JavaScript enabled"

The thing is I have a program that runs on javascript.
However my aim is to find an application that would let the user know whether he's got javascript disabled
But when i open checkjavascript.html (code above) with my browser (local/client) it doesnt give me javascript is not enabled.

I also tried to disable javascript through control panel/internet options without opening any browsers but it still remains the same.

Im not too sure about ASP dave, thanks for the link Ill have a look at it :)
0
 
GitchoCommented:
It still sounds like a browser issue, not a code issue.

When I follow the exact same scenario you outlined, i see "JavaScript is NOT enabled!" --- which leads me to believe that javascript is still enabled.

What browser client & version are u using ? What are the steps you are taking to disable scripts ?
0
 
ThiboAuthor Commented:
I am using xp client, IE 6.0

I used the normal steps to disable scripts:
Click on Tools
Scroll down to Internet Options
look up Security Tab
Click on Custom Level (Button)
Scroll to SCRIPTING section
Use Radio button select of Disable to turn Java Script support off

I do this via control panel/internet options after closing all windows...
0
 
frugleCommented:
are you changing the security settings for the Internet Zone or the Trusted Local Zone?

Internet Zone = web pages

Local Zone = pages run locally on your PC

Hope this helps

Mike
0
 
GitchoCommented:
When you get to the security tab, before you click "Custom", is "Internet" or "Local Internet" highlighted ?  Disable active scripting for both & try again.
0
 
cgarveyCommented:
>>It's not possible to redirect a page (using javascript, or any server-side language) to a different page if the client doesn't have javascript enabled.

>>The clients browser does not include any information in the headers about whether javascript is enabled or not when the client reqeusts a page.

>>Browsers can only read the code above and display alternate info in the <noscript> tags ...  

That's not *entirely* accurate. You can set a meta refresh to redirect to another page in say..15 seconds, and use javascript (if present) to redirect you elsewhere before the 15 seconds is up.
It's a filthy hack, but it works  :)

Colm
0
 
ThiboAuthor Commented:
I tried for both (Internet and local) but still no response...
0
 
ThiboAuthor Commented:
Cheers Gitcho for your help

See ya!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.