最近在做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);
}