检测 IE 浏览器并仅发出一次警报

发布于 2024-12-06 14:13:00 字数 1120 浏览 0 评论 0原文

我刚刚从各个站点获取了这段代码,并用我自己的代码来完成以下任务。

我想检测 IE 浏览器,并希望在整个会话期间仅提醒成员一次。我该怎么做?以下是我正在使用的代码。

<SCRIPT language="JavaScript">
<!--
var once_per_session=1
function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { // if cookie exists
offset += search.length
// set index of beginning of value
end = document.cookie.indexOf(";", offset);
// set index of end of cookie value
if (end == -1)
end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function alertornot(){
if (get_cookie('alerted')==''){
loadalert(alert)
document.cookie="alerted=yes"
}
}

function loadalert(){
var browserName=navigator.appName; 
if (browserName=="Microsoft Internet Explorer")
{
alert("Our Web Panel best viewed in Google Chrome and Firefox 3+ You may still continue but some features may not work properly..");
}
}

if (once_per_session==0)
loadalert()
else
alertornot()

//-->
</SCRIPT>

这真的可以通过检测浏览器来报警一次吗?请帮忙。

I just grabbed this code from various sites and came out with my own to accomplish the following.

I want to detect an IE Browser and would like to alert the members ONLY Once through out their session. How Do I Do this? Following is the code, which I'm using.

<SCRIPT language="JavaScript">
<!--
var once_per_session=1
function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { // if cookie exists
offset += search.length
// set index of beginning of value
end = document.cookie.indexOf(";", offset);
// set index of end of cookie value
if (end == -1)
end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function alertornot(){
if (get_cookie('alerted')==''){
loadalert(alert)
document.cookie="alerted=yes"
}
}

function loadalert(){
var browserName=navigator.appName; 
if (browserName=="Microsoft Internet Explorer")
{
alert("Our Web Panel best viewed in Google Chrome and Firefox 3+ You may still continue but some features may not work properly..");
}
}

if (once_per_session==0)
loadalert()
else
alertornot()

//-->
</SCRIPT>

Is this really possible to alert once by detecting the browser? Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

靖瑶 2024-12-13 14:13:00

您可以使用IE的条件注释。浏览器的 userAgent 不可靠。

#1:
<!--[if IE]>
You are using IE (IE5+ and above).
<![endif]-->

#2:
<![if !IE]>
You are NOT using IE.
<![endif]>

或者,您可以使用此技巧

var ie = !-[1,];
alert(ie);

我更喜欢 Conditional comments 解决方案,您可以选择其中之一

You can use Conditional comments of IE. Browser's userAgent is not reliable.

#1:
<!--[if IE]>
You are using IE (IE5+ and above).
<![endif]-->

#2:
<![if !IE]>
You are NOT using IE.
<![endif]>

Or, you can use this trick

var ie = !-[1,];
alert(ie);

I prefer the Conditional comments solution, you can choose either one

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文