頁面只能開啟一次Cooike如何?

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.