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.
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
精彩评论