How to detect Java and cookies whether enabled or not?

How to detect Java and cookies whether enabled or not?

Most of the websites using cookies to store data in the temporary file and java. Suppose in the system where you are accessing application cookies are disabled then what will happen? The application will not store any data to handle the request so that end user will get inconsistent data so that we should not allow the user and show the popup message to enable if cookies and JRE are disabled then we have doubt How to detect Java and cookies whether enabled or not?

Let’s us understand How to detect Java and cookies whether enabled or not? through given example.

Java Enabled Script

Use the navigator.javaEnabled() method. This method is supported in all major browsers (Internet Explorer, Firefox, Opera, Safari, Google Chrome).

Example

var javaOK = navigator.javaEnabled();
if (javaOK) document.write(''
  + 'Java is enabled: n'  + 'javaEnabled() returned '
  + javaOK.toString().bold()
 )
else document.write('' + 'Java is NOT enabled: n'
  + 'javaEnabled() returned ' + javaOK.toString().bold()
 )

Cookies Enabled Script

To test whether cookies are enabled, you can set a non-empty cookie and then retrieve it.

  • If the retrieved cookie value is the same as you’ve just set, then cookies are enabled.
  • If the retrieved value is an empty string, then cookies are disabled.

Example

How to Set Cookies

By using the following code we can set the cookies.

function SetCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}

How to Read Cookies

By using the following code we can read the stored cookies as above.

function ReadCookie(cookieName) {
 var theCookie=" "+document.cookie;
 var ind=theCookie.indexOf(" "+cookieName+"=");
 if (ind==-1) ind=theCookie.indexOf(";"+cookieName+"=");
 if (ind==-1 || cookieName=="") return "";
 var ind1=theCookie.indexOf(";",ind+1);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+2,ind1));
}

How To Test

Create one HTML file add the above java enabled script function and create button to call that script onClick event.

Create one HTML file add the above two functions and create two button to Set Cookies and Read Cookies and write onClick event.

I hope you guys understood How to detect Java and cookies whether enabled or not? If you have any suggestions please comment on it in the below. 🙂

Leave a Reply