最近在做webIM,嵌入到OA系统,由于WEBIM处在独立页面,所以如果多次点击就会出现多个页面,这样在IE6下,服务器推送会认不到页面.所以有了下面的代码:
复制代码 代码如下:
<script language="javascript" type="text/javascript">
window.onload = function () {
if (GetCookie("IsOpen") == "") {
SetCookie("IsOpen", "1");
StartService();
} else {
window.onunload = null;
window.opener = null;
window.open("", "_self");
window.close();
}
} window.onunload = function () {
if (GetCookie("IsOpen") != "")
DeleteCookie("IsOpen");
}
function GetCookieVal(offset) {
/// <summary>
/// //取得项名称为offset的cookie值
/// </summary>
/// <param name="name">Cookie名称</param>
var endstr = document.cookie.indexOf(";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie(name) {
/// <summary>
/// 根据名称获得Cookie值
/// </summary>
/// <param name="name">Cookie名称</param>
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return GetCookieVal(j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return "";
}
function SetCookie(name, value) {
/// <summary>
/// 设置Cookie
/// </summary>
/// <param name="name">Cookie名称</param>
/// <param name="value">Cookie值</param>
var argc = SetCookie.arguments.length;
var argv = SetCookie.arguments;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + value +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DeleteCookie(name) {
/// <summary>
/// 删除Cookie
/// </summary>
/// <param name="name">Cookie名称</param>
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval = GetCookie(name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
</script>
把上面的代码,放到head下面就可以了.呵呵够简单了吧?当然上面的代码主要解决的是用超链接打开的页面,如果是window.open打开的页面呢,那就更简单了,代码如下:
复制代码 代码如下:
var win = null;//定义一个变量去判断就可以了
if (win == null)
{
win=window.open("Main.aspx", '', strfeatures);
}
【页面只能打开一次Cooike如何实现】相关文章:
★ JavaScript实现点击自动选择TextArea文本的方法