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

4

1 回答 1

2

没有办法在服务器端检查它。您必须让客户端以某种方式通知服务器端。

在您的特定情况下,您可以让 JS 设置一个 cookie 并在服务器端检查它。

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

JS 将检查是否还没有 name=value of 的 cookie,js=true然后设置它并重新加载页面(以便发送 cookie)。请注意,当 JS 被禁用时,这将不会被执行。JSFrendered属性将检查具有名称的 cookiejs是否没有值,true然后相应地显示消息。一石两鸟:)

于 2012-05-02T12:51:33.247 回答