Skip to content Skip to sidebar Skip to footer

How To Check Whether Javascript Is Enabled In Client Browser Using Java Code

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

Solution 1:

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:

<scripttype="text/javascript">functioncreateCookie(name, value, days) {
    var expires = "";
    if (days) {
      var date = newDate();
      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>
 */privateboolean 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;
}

Solution 2:

In yourform for you can put code like this:

<noscript><inputtype="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:

booleanjavaScript= request.getParameter("JavaScript") == null;

Solution 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.

Solution 4:

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.

Solution 5:

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.

Post a Comment for "How To Check Whether Javascript Is Enabled In Client Browser Using Java Code"