Ensure that only the cooike version can be opened once on the page

Source: Internet
Author: User
Tags set cookie

Recently, webim is embedded in the OA system. Because webim is on an independent page, multiple pages will appear if you click it multiple times. In this way, the server push will not recognize the page under ie6. so with the followingCode:

    <  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>
// /// Obtain the cookie value whose name is offset
// /</Summary>
// /<Param name = "name"> cookie name </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>
// /Obtain the cookie value by name
// /</Summary>
// /<Param name = "name"> cookie name </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>
// /Set cookie
// /</Summary>
// /<Param name = "name"> cookie name </param>
// /<Param name = "value"> cookie value </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>
// /Delete cookie
// /</Summary>
// /<Param name = "name"> cookie name </param>
VaR Exp = New Date ();
Exp. settime (exp. gettime () - 1 );
VaR Cval = Getcookie (name );
Document. Cookie = Name + " = " + Cval + " ; Expires = " + Exp. togmtstring ();
}
< / SCRIPT>

Put the above Code under the head. Is it easy enough? Of course, the above Code mainly solves the problem of opening a page with a hyperlink. If it is a page opened by window. Open, it is simpler. The Code is as follows:

  var win  =    null  ; ///   define a variable for judgment.   
If (WIN = null )
{< br> win = window. open ( " main. aspx " , '' , strfeatures );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.