2016年1月30日 星期六

[研究] 檢查瀏覽器 JavaScript是否支援?是否啟用?

[研究] 檢查瀏覽器 JavaScript是否支援?是否啟用?

2016-01-30

參考
https://msdn.microsoft.com/en-us/library/system.web.httprequest.browser(v=vs.100).aspx

[ASP.NET][C#] Visual Studio 2015 實際測試


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            HttpBrowserCapabilities bc = Request.Browser;
            Response.Write("<p>Browser Capabilities:</p>");
            Response.Write("Type = " + bc.Type + "<br>");
            Response.Write("Name = " + bc.Browser + "<br>");
            Response.Write("Version = " + bc.Version + "<br>");
            Response.Write("Major Version = " + bc.MajorVersion + "<br>");
            Response.Write("Minor Version = " + bc.MinorVersion + "<br>");
            Response.Write("Platform = " + bc.Platform + "<br>");
            Response.Write("Is Beta = " + bc.Beta + "<br>");
            Response.Write("Is Crawler = " + bc.Crawler + "<br>");
            Response.Write("Is AOL = " + bc.AOL + "<br>");
            Response.Write("Is Win16 = " + bc.Win16 + "<br>");
            Response.Write("Is Win32 = " + bc.Win32 + "<br>");
            Response.Write("Supports Frames = " + bc.Frames + "<br>");
            Response.Write("Supports Tables = " + bc.Tables + "<br>");
            Response.Write("Supports Cookies = " + bc.Cookies + "<br>");
            Response.Write("Supports VB Script = " + bc.VBScript + "<br>");
            Response.Write("Supports JavaScript = " + bc.JavaScript + "<br>");
            Response.Write("Supports Java Applets = " + bc.JavaApplets + "<br>");
            Response.Write("Supports ActiveX Controls = " + bc.ActiveXControls + "<br>");
            Response.Write("CDF = " + bc.CDF + "<br>");
        }
    }
}


PS:Request.Browser.JavaScript is obselete (淘汰)?待研究

HttpCapabilitiesBase.JavaScript Property 已經被淘汰
https://msdn.microsoft.com/zh-tw/library/system.web.configuration.httpcapabilitiesbase.javascript(v=vs.110).aspx

PS:待研究


執行結果:

Browser Capabilities:
Type = InternetExplorer11
Name = InternetExplorer
Version = 11.0
Major Version = 11
Minor Version = 0
Platform = WinNT
Is Beta = False
Is Crawler = False
Is AOL = False
Is Win16 = False
Is Win32 = True
Supports Frames = True
Supports Tables = True
Supports Cookies = True
Supports VB Script = False
Supports JavaScript = True
Supports Java Applets = False
Supports ActiveX Controls = False
CDF = False

問題是,如果瀏覽器支援 JavaScript,但把它 Disable ,執行結果還是 True,無法分辨是 Enabled 或 Disabled。

********************************************************************************

[ASP.NET][C#]檢查瀏覽器是否啟用 JavaScript

參考這篇
How To Check Is JavaScript Enabled

[ C# ]


protected void Page_Load(object sender, EventArgs e)
{
    bool JavaScriptEnabled;

    if (Session["JavaScriptChecked"] == null)
    {
        Session["JavaScriptChecked"] = true;
        // call page again, but with CheckJavaScript=1 in query string
        Page.ClientScript.RegisterStartupScript(this.GetType(), "redirect",
            "window.location.href='" + Request.Url + "?CheckJavaScript=1" +
            "';", true);
    }
    if (Request.QueryString["CheckJavaScript"] == null)
        JavaScriptEnabled = false;
    else
        JavaScriptEnabled = true;
}

如果瀏覽器 JavaScript 是 Enabled,執行結果


JavaScript is enabled


如果瀏覽器 JavaScript 是 Disabled (有可能是不支援,或支援但沒啟用),執行結果


JavaScript is disabled


文中提到 FireFox 不支援
Response.Write(@"<script language="'javascript'" type='text/jscript'>" + 
               @"   window.location  = 'default.aspx?JScript=1'; </script>");

IE, Firefox, Safari, Opera 都支援
Page.ClientScript.RegisterStartupScript(this.GetType(), "redirect", 
           "window.location.href="/KB/aspnet/default.aspx"?JScript=1';", true); 


********************************************************************************

[JavaScript] 檢查瀏覽器是否啟用 JavaScript,如果不支援 JavaScript,重導到別的網頁

參考這篇

Check If the JavaScript is Enabled on the Client's Browser
http://aspadvice.com/blogs/azamsharp/archive/2007/09/30/Check-If-the-JavaScript-is-Enabled-on-the-Client_2700_s-Browser.aspx


<html>
<head>
<title>Untitled</title>
<noscript><meta http-equiv="refresh" content="0; url=DefaultNoJavaScript.html"></noscript>
</head>

<body onload="checkJavaScriptValidity()">
<script language="javascript" type="text/javascript">
  function checkJavaScriptValidity()
  {
    document.getElementById("jsEnabled").style.visibility = 'visible';
    document.getElementById("jsDisabled").style.visibility = 'hidden';
  }
</script>
<div id="jsEnabled" style="visibility:hidden">
  JavaScript is enabled
</div>
<div id="jsDisabled">
  JavaScript is disabled
</div>


PS:XHTML 5 不支援 <noscript> 
PS:HTML <noscript> Tag
http://www.w3schools.com/tags/tag_noscript.asp
In HTML 4.01, the <noscript> tag can only be used inside the <body> element.
In HTML5, the <noscript> tag can be used both inside <head> and <body>.
如果 <noscript> 放在 <head> 內, <noscript> 只能包含 <link> 、 <style> 、 <meta> 等元素。

********************************************************************************

[ASP.NET][C#] 如果不支援 JavaScript,重導到別的網頁


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.Browser.JavaScript)
            {
              Response.Redirect("DefalutNoJavaScript.aspx");
            }
        }
    }
}


PS:Request.Browser.JavaScript is obselete (淘汰)?待研究


********************************************************************************


[JavaScript] 如果不支援 JavaScript,重導到別的網頁

參考

How to determine whether Javascript is enabled?
http://therelentlessfrontend.com/2010/01/31/how-to-determine-whether-javascript-is-enabled/


<html>
<head>
<title>Untitled</title>

<noscript><meta http-equiv=”refresh” content=”0; url=whatyouwant.html”></noscript>

</head>
<body>
<form>
<!–this javascript ensures that anybody that doesnt use javascript wont see the menu–>
<script type=”text/javascript”>
<!–hide script from older browsers
document.write(‘<selectme=”whatever”><option>1</option><option>2</option></select>’);
–>
</script>
</form>
</body>
</html>



<body onload=”valid()”>
<form id=”form1″ runat=”server”>
<label Id=”lblWarning”>Your Browser has Disabled Scripting</label>
</form>
<script type=”text/jscript”>
function valid()
{
var lbl=document.getElementById(‘lblWarning’);
lbl.style.display = ‘none’;
}
</script>

</body>

(完)

相關

[研究][C#][ASP.NET][WebForm] 偵測瀏覽器類型(二)
http://shaurong.blogspot.com/2017/03/caspnetwebform.html

[研究][C#][ASP.NET][WebForm] 偵測瀏覽器類型(一)
http://shaurong.blogspot.com/2017/01/caspnet.html

[研究] 檢查瀏覽器 JavaScript是否支援?是否啟用?
http://shaurong.blogspot.com/2016/01/aspnetc-javascript.html

How To Check Is JavaScript Enabled

如何檢查使用者的瀏覽器是否關閉JavaScript和Cookie? 


HOW TO:在 ASP.NET Web 網頁中偵測瀏覽器類型


沒有留言:

張貼留言