6

can anyone help me in trying to check whether JavaScript is enabled in client browser using Java code.

0

6 Answers 6

11

Assuming you're writing a Java web application, one technique that I've used successfully is to have the first page that's accessed—typically a login form—write a session cookie when the page loads. Then have the Java code that the form submits to check for the existence of that cookie.

On the client:

<script type="text/javascript">
  function createCookie(name, value, days) {
    var expires = "";
    if (days) {
      var date = new Date();
      date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
      expires = "; expires=" + date.toGMTString();
    }
    var cookie = name + "=" + value + expires + "; path=" + "/";
    document.cookie = cookie;
  }
  createCookie("JavaScriptEnabledCheck", 1, 0);
</script>

On the server:

/**
 * Returns <code>true</code> if the session cookie set by the login form
 * is not present.
 * 
 * @param request The HTTP request being processed
 * @return <code>true</code> if JavaScript is disabled, otherwise <code>false</code>
 */
private boolean isJavaScriptDisabled(HttpServletRequest request)
{
  boolean isJavaScriptDisabled = true;
  Cookie[] cookies = request.getCookies();

  if (cookies != null)
  {
    for (int i = 0; i < cookies.length; i++)
    {
      if ("JavaScriptEnabledCheck".equalsIgnoreCase(cookies[i].getName()))
      {
        isJavaScriptDisabled = false;
        break;
      }
    }
  }

  return isJavaScriptDisabled;
}
Sign up to request clarification or add additional context in comments.

Comments

5

In yourform for you can put code like this:

<noscript>
  <input type="hidden" name="JavaScript" value="false" />
</noscript>

The parameter should only be submitted if the browser has scripts turned off. In your Java applications you can check it like so:

boolean javaScript = request.getParameter("JavaScript") == null;

1 Comment

I like this idea...but is it a cross-browser solution? It seems like something IE6 would love to mess up.
3

If a form submit is performed, you can put a hidden input in the form and fill out its value with javascript (from OnSubmit) and check that on the server side.

Comments

0

A simple thing would be to do a call back from the page, such as an AJAX call. I don't think there's any other way to determine this, at least not universally.

Comments

0

Are you trying to do this server-side or on the client in an applet?

If a browser does not support javascript (or has it turned off), it's highly unlikely they will have support for Java applets.

Comments

0

If you have PHP support just copy and paste the code below.

<noscript> 
  <input type="hidden" name="JavaScript" value="false" /> 
</noscript> 
/*This code is developed by Lankeshwer(nick name only) and can be used as open source by anyone*/
<?PHP
if($_POST['JavaScript']!='false'){
 echo "<run your javascript code here>";} //replace this line with your actual javascript code
else
 echo "<html><head><meta http-equiv="refresh" content="0;url=www.kharida.com"></head</html>";
?>

wish you a good luck and don't forget to make your code open

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.