Solved

Javascript detect

Posted on 2004-08-21
24
552 Views
Last Modified: 2013-11-19
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
0
Comment
Question by:Thibo
  • 9
  • 8
  • 2
  • +4
24 Comments
 
LVL 1

Expert Comment

by:MoonCalf
ID: 11858119
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
 
LVL 1

Expert Comment

by:MoonCalf
ID: 11858124
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
 

Author Comment

by:Thibo
ID: 11858214
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
 
LVL 2

Expert Comment

by:cgarvey
ID: 11858734
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
 

Author Comment

by:Thibo
ID: 11858991
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
 
LVL 13

Expert Comment

by:StormyWaters
ID: 11859502
You need to close the one where you disabled javascript from as well.
0
 
LVL 5

Expert Comment

by:Gitcho
ID: 11859747
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
 
LVL 5

Expert Comment

by:Gitcho
ID: 11859788
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
 

Author Comment

by:Thibo
ID: 11861809
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
 

Author Comment

by:Thibo
ID: 11861823
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
 
LVL 5

Expert Comment

by:Gitcho
ID: 11861828
i'll check the code locally ... 1 sec ...
0
 
LVL 5

Expert Comment

by:Gitcho
ID: 11861839
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:Thibo
ID: 11861976
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
 
LVL 5

Expert Comment

by:Gitcho
ID: 11862081
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
 
LVL 13

Expert Comment

by:davidlars99
ID: 11862211
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
 

Author Comment

by:Thibo
ID: 11862331
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
 
LVL 5

Expert Comment

by:Gitcho
ID: 11862567
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
 

Author Comment

by:Thibo
ID: 11863045
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
 
LVL 10

Expert Comment

by:frugle
ID: 11864587
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
 
LVL 5

Expert Comment

by:Gitcho
ID: 11865128
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
 
LVL 2

Expert Comment

by:cgarvey
ID: 11865784
>>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
 

Author Comment

by:Thibo
ID: 11877574
I tried for both (Internet and local) but still no response...
0
 
LVL 5

Accepted Solution

by:
Gitcho earned 500 total points
ID: 11878666
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
 

Author Comment

by:Thibo
ID: 12285820
Cheers Gitcho for your help

See ya!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Read about why website design really matters in today's demanding market.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now