If javascript is disabled in browser(four methods of judgment)

Lionsure 2020-06-05 Original by the website

In general, javascript are not disabled in client browsers, because many web pages rely on javascript. Without it, web pages cannot run normally, and problems such as web pages cannot be displayed completely and web pages are displayed disorderly; therefore, you don't need to check if javascript is disabled in browser under normal circumstances, but if you use javascript to protect the content of article, that is, use javascript code to prohibit users from copying. Once the javacritp is disabled, users can copy it.

Using javascript to protect the content of article is only a kind of lightweight protection, because users can still easily get the content of article, not to mention crawling with software tools, and directly viewing the source code of web page can also get the content of article; Maybe someone will say that it is forbidden to view the webpage source code. In fact, this cannot be prohibited at all. The "view webpage source code" in the right-click menu and the browser menu are also prohibited. You can also view it with Ctrl + U. Nevertheless, this method can still protect the article to a certain extent, because ordinary users do not know this.

If javascript is disabled in browser? There are usually many ways to judge. One method is: when the client opens the webpage, use javascript to assign a value to a hidden input (you can also save the value to a cookie), and then get this value on the server side, judging by whether the setting value is successful; another method is: when the client opens the webpage, use ajax to pass a value to the server, and judge based on whether the server can successfully get the value. The third method is to use noscript.

 

Method 1: Assign a value to a hidden input in javascript

Html code:

<input type="hidden" id="hidId" name="hidId" value="0" />
       <asp:Button ID="btn" runat="server" Text=" submit " OnClick="btn_Click" />

 

Javascript code:

<script type="text/javascript">
       function CheckJavascriptOn() {
              var obj = document.getElementById("hidId");
              obj.value = "1";
       }
       CheckJavascriptOn();
       </script>

 

Background code(C#):

protected void btn_Click(object sender, EventArgs e)
       {
              if(Request.Form["hidId"] == "1")
              Response.Write("Javascript is disabled in browser on the client");
       }

This method requires the user to submit the form before the server can get the value set by the client. The above code uses javascript to set the hidden input (hidId) value to 1 on the client. If the value of hidId is got on the server side, javascript is not disabled in browser on the client; If the value of hidId is 0, javascript is disabled in browser on the client.

 

Method 2: Save a value to cookies in javaScript

Javascript code:

function CheckJavascriptOn() {
              setCookies("JsIsOn", "1", 3);
       }

function setCookies(cookieName, val, expireHours) {
              var str = cookieName + "=" + escape(val);
               if (expireHours > 0) {
                     var date = new Date();
                     date.setTime(date.getTime + expireHours * 3600 * 1000);
                     str = str + "; expire=" + date.toGMTString();
              }
              document.cookie = str;
       }
       CheckJavascriptOn();

 

Background code(C#):

if (Request.Cookies["JsIsOn"] != null && Request.Cookies["JsIsOn"].Value == "1")
              Response.Write("Javascript is disabled in browser on the client");

Save the setting value with javascript in Cookies, which can be placed on any page without the user submitting the form; however, if the user disables Cookies, this method will not work, but there are not many users who disable Cookies.

 

Method 3: Pass the value set by javascript to the server through ajax

After the webpage is loaded, immediately use ajax to pass a value to the server. If the value can be passed normally, it means that javascript is not disabled on the client side. This method has a disadvantage. When a malicious user intentionally copies the URL to the browser address bar to access the page to determine whether to disable javascript, it is easy to cause a wrong judgment.

 

Javascript code:

var xmlhttp;
       function createXmlHttpObj() {
              try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
              catch (e) {
                     try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
                     catch (e) {
                            try { xmlhttp = new XMLHttpRequest(); }
                            catch (e) { }
                     }
              }
       }
       function JsIsOnCheck() {
              createXmlHttpObj();
              xmlhttp.open("get", "/test/JsIsOn.aspx?v=1", true);
              xmlhttp.onreadystatechange = function () {
                     if (xmlhttp.readyState == 4 && xmlhttp.status == 200 && xmlhttp.responseText != "") {
                            var resVal = xmlhttp.responseText;
                     }
              };
              xmlhttp.setRequestHeader("If-Modified-Since", "0");
              xmlhttp.send(null);
       }
       JsIsOnCheck();

 

The server-side judgment file (JsIsOn.aspx) code:

protected void Page_Load(object sender, EventArgs e)
       {
              string param = Request.QueryString["v"];
              if (!string.IsNullOrEmpty(param))
              {
                     if (param == "1")
                     {
                            //The client browser does not disable javaScript
                            Response.Clear();
                            Response.Write(param);
                            Response.Flush();
                            Response.End();
                     }
              }
       }

 

Method 4: Use noscript

When javascript is disabled on the client, <noscript></noscript> will be displayed. As long as a link can be automatically accessed to the server can be placed in it, the server can determine if javascript is disabled in browser on the client. Put the following code between <head></head> of the web page:

<noscript><meta http-equiv="refresh" content="0; url=IsDisable.aspx" /></noscript>

If javascript is disabled on the client, IsDisable.aspx is immediately accessed when the web page is opened; if javascript is not disabled on the client, IsDisable.aspx is not accessed when the web page is opened.