【发布时间】:2018-07-17 10:37:09
【问题描述】:
我正在使用 ASP.NET C#,在我的 viewsAll.cshtml 中我有一个 JavaScript
检测用户是否使用 Internet Explorer。alert("Other Browser"); 或 alert("Internet Explorer"); 工作正常。
问题是两个 c# 代码行都将被执行:@{ Session["BrowserName"] = "IE";} 和 @{Session["BrowserName"] = "other";}
但如果我使用 Internet Explorer,它应该只执行@{ Session["BrowserName"] = "IE";}
viewsAll.cshtml:
<script>
var usera = window.navigator.userAgent;
var ie = usera.indexOf("IE ");
if(ie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer
{
alert("Internet Explorer");
$('head').append('<link href="@Url.Content("~")Content/Styles/styleForIE.css" rel="stylesheet" />');
@{ Session["BrowserName"] = "IE";}
}
else{ // If Other Browser
alert("Other Browser");
$('head').append('<link href="@Url.Content("~")Content/Styles/styleForOther.css" rel="stylesheet" />');
@{Session["BrowserName"] = "other";}
}
</script>
【问题讨论】:
标签: javascript c# asp.net web-applications