Hướng dẫn asp.net check if javascript is enabled in browser - asp.net kiểm tra xem javascript có được bật trong trình duyệt không
Show
Giới thiệuTrong bài đăng này, tôi sẽ chỉ cho bạn cách phát hiện nếu máy khách có bật JavaScript hoặc vô hiệu hóa tại máy chủ web phụ trợ của bạn (trên bất kỳ nền tảng nào), để bạn có thể làm việc với nó trong mã phụ trợ của bạn và sử dụng nó như một boolean. Thật không may, không có cách nào ngoài hộp để phát hiện nó. Trình duyệt không gửi tiêu đề/siêu dữ liệu HTTP đến máy chủ web cho biết trình duyệt có bật hoặc tắt JavaScript không. Vì vậy, để phát hiện nó, chúng ta cần phải làm một công việc thủ công nhỏ. Làm thế nào để làm nóMã phụ trợBí quyết để phát hiện nó, là đặt cờ (cookie) trong trình duyệt máy khách cho biết trình duyệt có bật/tắt JavaScript không. Sau đó, máy chủ có thể nhìn vào cookie đó và kiểm tra xem JavaScript có cho biết JavaScript của nó được bật/tắt không. Trong ví dụ này, chúng ta hãy gọi Cookie this.IsJavascriptOn = GetIsJavascriptOn(); 1, nếu không JavaScript là this.IsJavascriptOn = GetIsJavascriptOn(); 2.Vì vậy, với mỗi this.IsJavascriptOn = GetIsJavascriptOn(); 3, hãy tạo một biến boolean trong máy chủ phụ trợ của bạn và gọi nó là this.IsJavascriptOn = GetIsJavascriptOn(); 4 và đặt nó thành this.IsJavascriptOn = GetIsJavascriptOn(); 0 nếu máy khách có cookie this.IsJavascriptOn = GetIsJavascriptOn(); 6 nếu không, sẽ đặt nó thành this.IsJavascriptOn = GetIsJavascriptOn(); 7.Tôi sẽ sử dụng ASP.NET MVC làm ví dụ mã, nhưng khái niệm này là giống nhau cho tất cả các nền tảng. Dưới đây là ví dụ về một hàm trả về this.IsJavascriptOn = GetIsJavascriptOn(); 0 nếu máy khách có cookie this.IsJavascriptOn = GetIsJavascriptOn(); 6, nếu không nó sẽ trả về this.IsJavascriptOn = GetIsJavascriptOn(); 7.bool GetIsJavascriptOn() { if (Request.Cookies.ContainsKey("hasjs") && Request.Cookies["hasjs"] == "false") return false; else return true; } Và sau đó ở đâu đó trong mã của chúng tôi, chúng tôi có thể đặt biến @if (Model.IsJavascriptOn == true) { <noscript> <meta http-equiv="Set-Cookie" content="hasjs=false; path=/"> <meta http-equiv="Refresh" content="0"> </noscript> } else //Client indicates javascript is disabled (cookie "hasjs=false" exists) { <script> document.cookie = "hasjs=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"; location.reload(true); </script> }1 của mình như bên dưới: this.IsJavascriptOn = GetIsJavascriptOn(); Và bây giờ, chúng tôi có Boolean xác định xem máy khách có bật/tắt JavaScript hay không, vì vậy bây giờ chúng tôi có thể sử dụng biến này ở mọi nơi trong mã backendcode của chúng tôi. Kết xuất HTMLVì vậy, bây giờ, chúng tôi cần phải đặt cookie this.IsJavascriptOn = GetIsJavascriptOn(); 6 nếu JavaScript bị tắt và xóa cookie this.IsJavascriptOn = GetIsJavascriptOn(); 6 nếu JavaScript được bật. Và điều đó đã được thực hiện khi chúng tôi hiển thị HTML.Chúng tôi phải hiển thị một số HTML tùy thuộc vào việc máy khách có chỉ ra rằng JavaScript được bật/tắt hay không. Và điều đó được hiển thị dưới đây. Chúng tôi sẽ đặt mã này vào bên trong: @if (Model.IsJavascriptOn == true) { <noscript> <meta http-equiv="Set-Cookie" content="hasjs=false; path=/"> <meta http-equiv="Refresh" content="0"> </noscript> } else //Client indicates javascript is disabled (cookie "hasjs=false" exists) { <script> document.cookie = "hasjs=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"; location.reload(true); </script> } Bây giờ chúng tôi thực sự đã hoàn thành. Nếu bạn chỉ muốn kiểm tra nó và không muốn bất kỳ lời giải thích nào, bạn có thể bỏ qua phần còn lại. Ở đây, chúng tôi vẽ khối nếu biến @if (Model.IsJavascriptOn == true) { <noscript> <meta http-equiv="Set-Cookie" content="hasjs=false; path=/"> <meta http-equiv="Refresh" content="0"> </noscript> } else //Client indicates javascript is disabled (cookie "hasjs=false" exists) { <script> document.cookie = "hasjs=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"; location.reload(true); </script> }4Equals this.IsJavascriptOn = GetIsJavascriptOn(); 7 của chúng tôi, nếu không chúng tôi vẽ @if (Model.IsJavascriptOn == true) { <noscript> <meta http-equiv="Set-Cookie" content="hasjs=false; path=/"> <meta http-equiv="Refresh" content="0"> </noscript> } else //Client indicates javascript is disabled (cookie "hasjs=false" exists) { <script> document.cookie = "hasjs=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"; location.reload(true); </script> }6Block. block if our variable @if (Model.IsJavascriptOn == true) { <noscript> <meta http-equiv="Set-Cookie" content="hasjs=false; path=/"> <meta http-equiv="Refresh" content="0"> </noscript> } else //Client indicates javascript is disabled (cookie "hasjs=false" exists) { <script> document.cookie = "hasjs=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"; location.reload(true); </script> }4equals this.IsJavascriptOn = GetIsJavascriptOn(); 7, otherwise we draw the @if (Model.IsJavascriptOn == true) { <noscript> <meta http-equiv="Set-Cookie" content="hasjs=false; path=/"> <meta http-equiv="Refresh" content="0"> </noscript> } else //Client indicates javascript is disabled (cookie "hasjs=false" exists) { <script> document.cookie = "hasjs=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"; location.reload(true); </script> }6block. @if (Model.IsJavascriptOn == true) { <noscript> <meta http-equiv="Set-Cookie" content="hasjs=false; path=/"> <meta http-equiv="Refresh" content="0"> </noscript> } else //Client indicates javascript is disabled (cookie "hasjs=false" exists) { <script> document.cookie = "hasjs=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"; location.reload(true); </script> }7Block sẽ đặt cookie this.IsJavascriptOn = GetIsJavascriptOn(); 6 và sau đó làm mới trang (vâng, bạn có thể đặt cookie và làm mới trang mà không cần JavaScript).Khối @if (Model.IsJavascriptOn == true) { <noscript> <meta http-equiv="Set-Cookie" content="hasjs=false; path=/"> <meta http-equiv="Refresh" content="0"> </noscript> } else //Client indicates javascript is disabled (cookie "hasjs=false" exists) { <script> document.cookie = "hasjs=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"; location.reload(true); </script> }9 sẽ xóa cookie this.IsJavascriptOn = GetIsJavascriptOn(); 6 và sau đó làm mới trang.Vì vậy, những gì xảy ra ở đây, là nếu máy khách chỉ ra rằng JavaScript đang bật, thì chúng tôi vẽ khối using Microsoft.AspNetCore.Mvc; namespace WebApplication1 { public class ViewDataModel { public bool IsJavascriptOn; } } namespace WebApplication1.Controllers { public class HomeController : Controller { public ViewDataModel ViewDataModel; bool GetIsJavascriptOn() { if (Request.Cookies.ContainsKey("hasjs") && Request.Cookies["hasjs"] == "false") return false; else return true; } public IActionResult Index() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } public IActionResult About() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } public IActionResult Contact() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } } }1. Nhưng máy khách sẽ không chạy khối using Microsoft.AspNetCore.Mvc; namespace WebApplication1 { public class ViewDataModel { public bool IsJavascriptOn; } } namespace WebApplication1.Controllers { public class HomeController : Controller { public ViewDataModel ViewDataModel; bool GetIsJavascriptOn() { if (Request.Cookies.ContainsKey("hasjs") && Request.Cookies["hasjs"] == "false") return false; else return true; } public IActionResult Index() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } public IActionResult About() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } public IActionResult Contact() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } } }1, trừ khi nó thực sự bị vô hiệu hóa. Vì vậy, nếu nó chạy khối using Microsoft.AspNetCore.Mvc; namespace WebApplication1 { public class ViewDataModel { public bool IsJavascriptOn; } } namespace WebApplication1.Controllers { public class HomeController : Controller { public ViewDataModel ViewDataModel; bool GetIsJavascriptOn() { if (Request.Cookies.ContainsKey("hasjs") && Request.Cookies["hasjs"] == "false") return false; else return true; } public IActionResult Index() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } public IActionResult About() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } public IActionResult Contact() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } } }1, thì điều đó có nghĩa là máy khách đã nói với máy chủ rằng nó đã bật JavaScript, nhưng đã sai và cần cập nhật/đặt using Microsoft.AspNetCore.Mvc; namespace WebApplication1 { public class ViewDataModel { public bool IsJavascriptOn; } } namespace WebApplication1.Controllers { public class HomeController : Controller { public ViewDataModel ViewDataModel; bool GetIsJavascriptOn() { if (Request.Cookies.ContainsKey("hasjs") && Request.Cookies["hasjs"] == "false") return false; else return true; } public IActionResult Index() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } public IActionResult About() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } public IActionResult Contact() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } } }4TO this.IsJavascriptOn = GetIsJavascriptOn(); 0 và sau đó làm mới trang.Sau đó, sau khi nó đã thực hiện điều đó, máy chủ sẽ hiển thị khối @if (Model.IsJavascriptOn == true) { <noscript> <meta http-equiv="Set-Cookie" content="hasjs=false; path=/"> <meta http-equiv="Refresh" content="0"> </noscript> } else //Client indicates javascript is disabled (cookie "hasjs=false" exists) { <script> document.cookie = "hasjs=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"; location.reload(true); </script> }9 thay thế và sau đó máy khách sẽ không bao giờ chạy khối @if (Model.IsJavascriptOn == true) { <noscript> <meta http-equiv="Set-Cookie" content="hasjs=false; path=/"> <meta http-equiv="Refresh" content="0"> </noscript> } else //Client indicates javascript is disabled (cookie "hasjs=false" exists) { <script> document.cookie = "hasjs=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"; location.reload(true); </script> }9 cho đến khi nó cho phép lại. Vì vậy, khi máy khách bật lại JavaScript, thì máy khách sẽ chạy khối @if (Model.IsJavascriptOn == true) { <noscript> <meta http-equiv="Set-Cookie" content="hasjs=false; path=/"> <meta http-equiv="Refresh" content="0"> </noscript> } else //Client indicates javascript is disabled (cookie "hasjs=false" exists) { <script> document.cookie = "hasjs=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"; location.reload(true); </script> }9, xóa cookie using Microsoft.AspNetCore.Mvc; namespace WebApplication1 { public class ViewDataModel { public bool IsJavascriptOn; } } namespace WebApplication1.Controllers { public class HomeController : Controller { public ViewDataModel ViewDataModel; bool GetIsJavascriptOn() { if (Request.Cookies.ContainsKey("hasjs") && Request.Cookies["hasjs"] == "false") return false; else return true; } public IActionResult Index() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } public IActionResult About() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } public IActionResult Contact() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } } }4and làm mới trang. Và sau đó, máy chủ sẽ vẽ khối using Microsoft.AspNetCore.Mvc; namespace WebApplication1 { public class ViewDataModel { public bool IsJavascriptOn; } } namespace WebApplication1.Controllers { public class HomeController : Controller { public ViewDataModel ViewDataModel; bool GetIsJavascriptOn() { if (Request.Cookies.ContainsKey("hasjs") && Request.Cookies["hasjs"] == "false") return false; else return true; } public IActionResult Index() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } public IActionResult About() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } public IActionResult Contact() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } } }1 và sau đó máy khách sẽ không bao giờ chạy khối đó cho đến khi máy khách vô hiệu hóa JavaScript nữa. Và đó là cách chu kỳ đi. Ví dụ đầy đủ với ASP.NET MVCDưới đây là một ví dụ đầy đủ trong ASP.NET Core MVC (.NET Framework). Nó khá đơn giản, vì vậy hy vọng, các lập trình viên không phải Asp.net sẽ hiểu nó, vì nó gần giống như mã giả. Trong ví dụ này, tôi sẽ hiển thị một trang web tối thiểu với chỉ số 3 trang ( @model ViewDataModel <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Detect_Javascript</title> @if (Model.IsJavascriptOn == true) { <noscript> <meta http-equiv="Set-Cookie" content="hasjs=false; path=/"> <meta http-equiv="Refresh" content="0"> </noscript> } else //Client indicates javascript is disabled (cookie "hasjs=false" exists) { <script> document.cookie = "hasjs=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"; location.reload(true); </script> } </head> <body> <ul> <li><a href="/home">Home</a></li> <li><a href="/home/about">About</a></li> <li><a href="/home/contact">Contact</a></li> </ul> @RenderBody() </body> </html>1), liên hệ và về không có CSS. HomeController.csTệp này là điểm bắt đầu của nơi mã bắt đầu khi khách hàng yêu cầu một trang trên một đường dẫn cụ thể. Nó sẽ kiểm tra xem máy khách có cookie this.IsJavascriptOn = GetIsJavascriptOn(); 6 không và đặt biến @if (Model.IsJavascriptOn == true) { <noscript> <meta http-equiv="Set-Cookie" content="hasjs=false; path=/"> <meta http-equiv="Refresh" content="0"> </noscript> } else //Client indicates javascript is disabled (cookie "hasjs=false" exists) { <script> document.cookie = "hasjs=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"; location.reload(true); </script> }1 của chúng tôi thành this.IsJavascriptOn = GetIsJavascriptOn(); 0 nếu nó tồn tại, nếu không sẽ đặt nó thành this.IsJavascriptOn = GetIsJavascriptOn(); 7.using Microsoft.AspNetCore.Mvc; namespace WebApplication1 { public class ViewDataModel { public bool IsJavascriptOn; } } namespace WebApplication1.Controllers { public class HomeController : Controller { public ViewDataModel ViewDataModel; bool GetIsJavascriptOn() { if (Request.Cookies.ContainsKey("hasjs") && Request.Cookies["hasjs"] == "false") return false; else return true; } public IActionResult Index() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } public IActionResult About() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } public IActionResult Contact() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } } } _Layout.cshtmlĐây là điểm khởi đầu khi chúng tôi hiển thị HTML của mình. @model ViewDataModel <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Detect_Javascript</title> @if (Model.IsJavascriptOn == true) { <noscript> <meta http-equiv="Set-Cookie" content="hasjs=false; path=/"> <meta http-equiv="Refresh" content="0"> </noscript> } else //Client indicates javascript is disabled (cookie "hasjs=false" exists) { <script> document.cookie = "hasjs=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"; location.reload(true); </script> } </head> <body> <ul> <li><a href="/home">Home</a></li> <li><a href="/home/about">About</a></li> <li><a href="/home/contact">Contact</a></li> </ul> @RenderBody() </body> </html>6 là những gì được hiển thị bất kể chúng tôi đang ở trang nào, và sau đó nó kích hoạt @model ViewDataModel <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Detect_Javascript</title> @if (Model.IsJavascriptOn == true) { <noscript> <meta http-equiv="Set-Cookie" content="hasjs=false; path=/"> <meta http-equiv="Refresh" content="0"> </noscript> } else //Client indicates javascript is disabled (cookie "hasjs=false" exists) { <script> document.cookie = "hasjs=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"; location.reload(true); </script> } </head> <body> <ul> <li><a href="/home">Home</a></li> <li><a href="/home/about">About</a></li> <li><a href="/home/contact">Contact</a></li> </ul> @RenderBody() </body> </html>7, một hàm MVC ASP.NET sẽ hiển thị trang chúng tôi đang bật. Và điều đó sẽ kết xuất trong trường hợp của chúng tôi hoặc là index.cshtml, contact.cshtml hoặc about.cshtml. @model ViewDataModel <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Detect_Javascript</title> @if (Model.IsJavascriptOn == true) { <noscript> <meta http-equiv="Set-Cookie" content="hasjs=false; path=/"> <meta http-equiv="Refresh" content="0"> </noscript> } else //Client indicates javascript is disabled (cookie "hasjs=false" exists) { <script> document.cookie = "hasjs=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"; location.reload(true); </script> } </head> <body> <ul> <li><a href="/home">Home</a></li> <li><a href="/home/about">About</a></li> <li><a href="/home/contact">Contact</a></li> </ul> @RenderBody() </body> </html> Index.cshtml@model ViewDataModel <h2>Home page</h2> <p>This is the home page</p> @if (Model.IsJavascriptOn == true) { <p>Javascript is enabled</p> } else { <p>Javascript is disabled</p> } Contact.cshtml@model ViewDataModel <h2>Contact page</h2> <p>This is the contact page</p> @if (Model.IsJavascriptOn == true) { <p>Javascript is enabled</p> } else { <p>Javascript is disabled</p> } About.cshtml@model ViewDataModel <h2>About page</h2> <p>This is the about page</p> @if (Model.IsJavascriptOn == true) { <p>Javascript is enabled</p> } else { <p>Javascript is disabled</p> } Vì vậy, ở đây, nếu bạn có JavaScript, thì nó sẽ vẽ @model ViewDataModel <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Detect_Javascript</title> @if (Model.IsJavascriptOn == true) { <noscript> <meta http-equiv="Set-Cookie" content="hasjs=false; path=/"> <meta http-equiv="Refresh" content="0"> </noscript> } else //Client indicates javascript is disabled (cookie "hasjs=false" exists) { <script> document.cookie = "hasjs=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"; location.reload(true); </script> } </head> <body> <ul> <li><a href="/home">Home</a></li> <li><a href="/home/about">About</a></li> <li><a href="/home/contact">Contact</a></li> </ul> @RenderBody() </body> </html>8 nếu không, @model ViewDataModel <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Detect_Javascript</title> @if (Model.IsJavascriptOn == true) { <noscript> <meta http-equiv="Set-Cookie" content="hasjs=false; path=/"> <meta http-equiv="Refresh" content="0"> </noscript> } else //Client indicates javascript is disabled (cookie "hasjs=false" exists) { <script> document.cookie = "hasjs=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"; location.reload(true); </script> } </head> <body> <ul> <li><a href="/home">Home</a></li> <li><a href="/home/about">About</a></li> <li><a href="/home/contact">Contact</a></li> </ul> @RenderBody() </body> </html>9. Dưới đây là hai ví dụ về cách nó nên nhìn: Lưu ý cuối cùngBên trong using Microsoft.AspNetCore.Mvc; namespace WebApplication1 { public class ViewDataModel { public bool IsJavascriptOn; } } namespace WebApplication1.Controllers { public class HomeController : Controller { public ViewDataModel ViewDataModel; bool GetIsJavascriptOn() { if (Request.Cookies.ContainsKey("hasjs") && Request.Cookies["hasjs"] == "false") return false; else return true; } public IActionResult Index() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } public IActionResult About() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } public IActionResult Contact() { this.ViewDataModel = new ViewDataModel(); this.ViewDataModel.IsJavascriptOn = this.GetIsJavascriptOn(); return View(this.ViewDataModel); } } }1, thẻ @model ViewDataModel <h2>Home page</h2> <p>This is the home page</p> @if (Model.IsJavascriptOn == true) { <p>Javascript is enabled</p> } else { <p>Javascript is disabled</p> }1, điều rất quan trọng là đặt đường dẫn thành " @model ViewDataModel <h2>Home page</h2> <p>This is the home page</p> @if (Model.IsJavascriptOn == true) { <p>Javascript is enabled</p> } else { <p>Javascript is disabled</p> }2, vì cookie có thể có đường dẫn, vì vậy nếu bạn điều hướng đến các đường dẫn/trang web khác nhau trong trang web, thì máy khách có thể tạo nhiều cookie this.IsJavascriptOn = GetIsJavascriptOn(); 6 khác nhau tùy thuộc vào đó tùy thuộc vào đó Đường dẫn khách hàng đang đi.Cảm ơn bạn đã đọc, tôi hy vọng điều này sẽ hữu ích cho rất nhiều bạn. :-) Giấy phépĐược viết bởi Denmark Thành viên này chưa cung cấp tiểu sử. Giả sử nó thú vị và đa dạng, và có lẽ là một cái gì đó để làm với lập trình. Nhận xét và thảo luận
Sử dụng Ctrl+trái/phải để chuyển đổi tin nhắn, Ctrl+lên/xuống để chuyển đổi các luồng, Ctrl+Shift+Left/Right để chuyển đổi trang. Điều gì sẽ xảy ra nếu JavaScript bị vô hiệu hóa trong trình duyệt?Nếu JavaScript đã bị vô hiệu hóa trong trình duyệt của bạn, nội dung hoặc chức năng của trang web có thể bị giới hạn hoặc không có sẵn.the content or the functionality of the web page can be limited or unavailable.
JavaScript có được thực hiện bởi trình duyệt hoặc máy chủ không?JavaScript thường được chạy trên các trang web bên trong trình duyệt, nhưng nó cũng có thể là chạy phía máy chủ. Chúng tôi sẽ đi qua một số nền tảng này sau.most often run on webpages inside the browser, but it can also be run server-side. We will go through some of these platforms later.
Điều gì xảy ra nếu JavaScript được bật?Nếu JavaScript được bật thì nó sẽ cập nhật câu trả lời "Không" mặc định cho câu trả lời "Có"! (Vì vậy, nếu JavaScript không được bật, "Không" mặc định vẫn là câu trả lời).it updates the default "No" answer in to a "Yes" answer! (so that if JavaScript is not enabled, the default "No" remains as the answer).
JavaScript có được bật theo mặc định không?JavaScript được sử dụng rộng rãi trong tất cả các phiên bản mới hơn của trình duyệt web (Chrome, Firefox, Internet Explorer, Safari, Opera), nơi nó được bật theo mặc định.it is enabled by default. |