Use the Javascript parent window to control only one child window

Source: Internet
Author: User
Tags setcookie

Recently, some questions about how to control a child window in the parent window are frequently circulated on the Internet. I have checked some questions about how to control the child window in the parent window. Most of them can only solve some or very troublesome issues, but they are not perfect. For example, a cookie is used. The following is an example.

A pop-up window is made on the home page. How to make it only pop up once is not displayed when you return to the home page.

---------------------------------------------------------------

<SCRIPT>

Window. Open ("http://www.yeboss.com.cn", "yeboss", "width = 300, Height = 400 ")

</SCRIPT>

---------------------------------------------------------------

Cookie

---------------------------------------------------------------

<Head>

<Script language = "JavaScript">

<! -- Begin

VaR expdays = 1; // Number of days the cookie shocould last

VaR page = "only-popup-once.html ";

VaR windowprops = "width = 300, Height = 200, location = No, toolbar = No, menubar = No, scrollbars = No, resizable = yes ";

Function getcookie (name ){

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 NULL;

}

Function setcookie (name, value ){

VaR argv = setcookie. arguments;

VaR argc = setcookie. Arguments. length;

VaR expires = (argc> 2 )? Argv [2]: NULL;

VaR Path = (argc> 3 )? Argv [3]: NULL;

VaR domain = (argc> 4 )? Argv [4]: NULL;

VaR secure = (argc> 5 )? Argv [5]: false;

Document. Cookie = Name + "=" + escape (value) +

(Expires = NULL )? "": ("; Expires =" + expires. togmtstring () +

(Path = NULL )? "": ("; Path =" + path) +

(Domain = NULL )? "": ("; Domain =" + domain) +

(Secure = true )? "; Secure ":"");

}

Function deletecookie (name ){

VaR exp = new date ();

Exp. settime (exp. gettime ()-1 );

VaR cval = getcookie (name );

Document. Cookie = Name + "=" + cval + "; expires =" + exp. togmtstring ();

}

VaR exp = new date ();

Exp. settime (exp. gettime () + (expdays * 24*60*60*1000 ));

Function Amt (){

VaR COUNT = getcookie (count)

If (COUNT = NULL ){

Setcookie (count, 1)

Return 1

}

Else {

VaR newcount = parseint (count) + 1;

Deletecookie (count)

Setcookie (count, newcount, exp)

Return count

}

}

Function getcookieval (offset ){

VaR endstr = Document. Cookie. indexof (";", offset );

If (endstr =-1)

Endstr = Document. Cookie. length;

Return Unescape (document. Cookie. substring (offset, endstr ));

}

Function checkcount (){

VaR COUNT = getcookie (count );

If (COUNT = NULL ){

Count = 1;

Setcookie (count, Count, exp );

Window. Open (page, "", windowprops );

}

Else {

Count ++;

Setcookie (count, Count, exp );

}

}

// End -->

</SCRIPT>

<Body onload = "checkcount ()">

This is an example taken on the Internet. Is it difficult to understand many of them? I will introduce a new method to you to make sure it looks good.

<Script language = "JavaScript" type = "text/JavaScript">

<! -- Copyright Fenggang at 2007/3/23

VaR W = NULL;

Function mm_openbrwindow (theurl, winname, features) {// V2.0

If (W! = Undefined & isopen ()){

W. Close ();

}

W = Window. Open ("", winname, features );

W. Location. Replace (theurl );

}

Function isopen ()

{

Try

{

W.doc ument;

Return true;

}

Catch (Ex)

{}

Return false;

}

// -->

</SCRIPT>

The mm_openbrwindow () function name is a common pop-up window function name in Dreamweaver. Code Put in the page

Then, you can only find one window in the pop-up window. The above is the essence. below is the code of a complete test page. Try it.

<% @ Language = "VBScript" codePage = "936" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>

<Title> untitled document </title>

<Script language = "JavaScript" type = "text/JavaScript">

<! -- Copyright Fenggang at 2007/3/23

VaR W = NULL;

Function mm_openbrwindow (theurl, winname, features) {// V2.0

If (W! = Undefined & isopen ()){

W. Close ();

}

W = Window. Open ("", winname, features );

W. Location. Replace (theurl );

}

Function isopen ()

{

Try

{

W.doc ument;

Return true;

}

Catch (Ex)

{}

Return false;

}

// -->

</SCRIPT>

</Head>

<Body>

<A href = "javascript:;" onclick = "mm_openbrwindow (show. ASP, scrollbars = Yes, resizable = Yes, width = 570, Height = 500) "> title </a>

</Body>

</Html>

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.