3

I want to know that is there any way by which i can detect whether Javascript is enabled in the browser. Like we can detect cookies by using code in the filter

if (httpServletRequest.isRequestedSessionIdFromCookie()) {

}

can i detect javascript also? Does Java provide any method for this? Actually i want to check whether both javascript and Cookies are enabled. If not then i want to show message to user that Please make sure your cookies and javascript is enable

Thanks

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Basit
  • 8,426
  • 46
  • 116
  • 196
  • Check it : [http://stackoverflow.com/questions/121203/how-to-detect-if-javascript-is-disabled][1] [1]: http://stackoverflow.com/questions/121203/how-to-detect-if-javascript-is-disabled – swati May 02 '12 at 10:19

1 Answers1

2

There's no way to check it in the server side. You'd have to let the client notify the server side about it somehow.

In your particular case, you could just let JS set a cookie and check it in the server side.

<h:outputScript>
    if (document.cookie.indexOf('js=true') == -1) {
        document.cookie = 'js=true';
        window.location.reload(true);
    }
</h:outputScript>
<h:panelGroup rendered="#{not cookie.js.value}">
    Please make sure that Cookies and JavaScript are enabled.
</h:panelGroup>

The JS will check if there isn't already a cookie with name=value of js=true and then set it and reload the page (so that the cookie will be sent). Note that this will thus not be executed when JS is disabled. The JSF rendered attribute will check if the cookie with the name js hasn't a value of true and then display the message accordingly. Two birds with one stone :)

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555