如何检测Javascript是否已启用

时间:2021-06-22 15:45:11

I have inhereted an application Asp.net C# application that I need to deploy. It was written 18 months ago by a contractor and never deployed till now. The code behind file on our login page has this bit of code:

我已经内置了我需要部署的应用程序Asp.net C#应用程序。它是18个月前由承包商编写的,直到现在才部署。我们登录页面上的代码隐藏文件包含以下代码:

            //if for some reason javascript is not enabled, redirect back to login page
        sb.AppendLine( "<head><meta http-equiv=\"refresh\" content=\"30;URL=./?logout=1\"><link rel=\"Stylesheet\" href=\"./Styles/Main.css\" type=\"text/css\" /></head>" );
        sb.AppendLine( "<body onload=\"document.forms['loginForm'].submit()\">" );

I can't see how those two strings accomplish what the comment says they accomplish and I'm not sure what they're even doing actually. Should there be an 'if' statement wrapped around all that?

我看不出这两个字符串是如何完成评论所说的那样,而且我不确定他们甚至在做什么。是否应该有一个'if'声明?

2 个解决方案

#1


1  

<meta http-equiv="refresh" content="30;URL=./?logout=1">

Tells the browser to redirect to ./?logout=1 in 30 seconds.

告诉浏览器在30秒内重定向到./?logout=1。

<body onload="document.forms['loginForm'].submit()">

Looks for a form called loginForm and submits it.

查找名为loginForm的表单并提交它。

If the page loads and and the form submits in less than thirty seconds, everything will work as expected. If not, it assume JavaScript is not enabled. This is a bad solution. Users without Javascript have to wait 30 seconds before being redirected. And there is a real chance of a false positive over a slow network connection.

如果页面加载并且表单在不到30秒内提交,则一切都将按预期工作。如果没有,则假定未启用JavaScript。这是一个糟糕的解决方案。没有Javascript的用户必须等待30秒才能被重定向。并且在网络连接缓慢的情况下存在误报的可能性。

Try the noscript tag:

试试noscript标签:

<noscript><meta http-equiv="refresh" content="0;URL=./?logout=1"></noscript>

#2


0  

It looks to me like it is using javascript to submit the login form and that will not happen if javascript is not enabled, in which case the meta tag is going to refresh the page and logout.

它看起来像是使用javascript来提交登录表单,如果没有启用javascript则不会发生,在这种情况下,元标记将刷新页面并注销。

#1


1  

<meta http-equiv="refresh" content="30;URL=./?logout=1">

Tells the browser to redirect to ./?logout=1 in 30 seconds.

告诉浏览器在30秒内重定向到./?logout=1。

<body onload="document.forms['loginForm'].submit()">

Looks for a form called loginForm and submits it.

查找名为loginForm的表单并提交它。

If the page loads and and the form submits in less than thirty seconds, everything will work as expected. If not, it assume JavaScript is not enabled. This is a bad solution. Users without Javascript have to wait 30 seconds before being redirected. And there is a real chance of a false positive over a slow network connection.

如果页面加载并且表单在不到30秒内提交,则一切都将按预期工作。如果没有,则假定未启用JavaScript。这是一个糟糕的解决方案。没有Javascript的用户必须等待30秒才能被重定向。并且在网络连接缓慢的情况下存在误报的可能性。

Try the noscript tag:

试试noscript标签:

<noscript><meta http-equiv="refresh" content="0;URL=./?logout=1"></noscript>

#2


0  

It looks to me like it is using javascript to submit the login form and that will not happen if javascript is not enabled, in which case the meta tag is going to refresh the page and logout.

它看起来像是使用javascript来提交登录表单,如果没有启用javascript则不会发生,在这种情况下,元标记将刷新页面并注销。