手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >用javascript父窗口控制只弹出一个子窗口
用javascript父窗口控制只弹出一个子窗口
摘要:近来网上经常流传一些关于如何在父窗口控制只弹出一个子窗口问题,我查看了一些,大部分都是只能解决部分或者很麻烦,还不算完美。比如有人是实现的这...

近来网上经常流传一些关于如何在父窗口控制只弹出一个子窗口问题,我查看了一些,大部分都是只能解决部分或者很麻烦,还不算完美。比如有人是实现的这样,使用的是Cookie,以下是例子。

主页上做了一个弹出窗口,如何使其只弹出一次,返回主页时不再弹出了。

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

<script>

window.open("http://www.yeboss.com.cn","yeboss","width=300,height=400")

</script>

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

cookie

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

<HEAD>

<SCRIPTLANGUAGE="JavaScript">

<!--Begin

varexpDays=1;//numberofdaysthecookieshouldlast

varpage="only-popup-once.html";

varwindowprops="width=300,height=200,location=no,toolbar=no,menubar=no,scrollbars=no,resizable=yes";

functionGetCookie(name){

vararg=name+"=";

varalen=arg.length;

varclen=document.cookie.length;

vari=0;

while(i<clen){

varj=i+alen;

if(document.cookie.substring(i,j)==arg)

returngetCookieVal(j);

i=document.cookie.indexOf("",i)+1;

if(i==0)break;

}

returnnull;

}

functionSetCookie(name,value){

varargv=SetCookie.arguments;

varargc=SetCookie.arguments.length;

varexpires=(argc>2)?argv[2]:null;

varpath=(argc>3)?argv[3]:null;

vardomain=(argc>4)?argv[4]:null;

varsecure=(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":"");

}

functionDeleteCookie(name){

varexp=newDate();

exp.setTime(exp.getTime()-1);

varcval=GetCookie(name);

document.cookie=name+"="+cval+";expires="+exp.toGMTString();

}

varexp=newDate();

exp.setTime(exp.getTime()+(expDays*24*60*60*1000));

functionamt(){

varcount=GetCookie(’count’)

if(count==null){

SetCookie(’count’,’1’)

return1

}

else{

varnewcount=parseInt(count)+1;

DeleteCookie(’count’)

SetCookie(’count’,newcount,exp)

returncount

}

}

functiongetCookieVal(offset){

varendstr=document.cookie.indexOf(";",offset);

if(endstr==-1)

endstr=document.cookie.length;

returnunescape(document.cookie.substring(offset,endstr));

}

functioncheckCount(){

varcount=GetCookie(’count’);

if(count==null){

count=1;

SetCookie(’count’,count,exp);

window.open(page,"",windowprops);

}

else{

count++;

SetCookie(’count’,count,exp);

}

}

//End-->

</script>

<BODYOnLoad="checkCount()">

这个是网上随便摘的一个例子,是不是感觉很多也很难看懂呢,下面我给大家说一种新方法,保证好看好使。

<scriptlanguage="JavaScript"type="text/JavaScript">

<!--Copyrightfenggangat2007/3/23

varw=null;

functionMM_openBrWindow(theURL,winName,features){//v2.0

if(w!=undefined&&isOpen()){

w.close();

}

w=window.open("",winName,features);

w.location.replace(theURL);

}

functionisOpen()

{

try

{

w.document;

returntrue;

}

catch(ex)

{}

returnfalse;

}

//-->

</script>

这个MM_openBrWindow()函数名是在Dreamweaver中常用的弹出窗口函数名,将这段代码放进页面里

然后再做弹出窗口就只能探出一个窗口了,以上是精髓部分,下面是完整的一个测试页的代码,试试看吧。

<%@LANGUAGE="VBSCRIPT"CODEPAGE="936"%>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

<head>

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

<title>无标题文档</title>

<scriptlanguage="JavaScript"type="text/JavaScript">

<!--Copyrightfenggangat2007/3/23

varw=null;

functionMM_openBrWindow(theURL,winName,features){//v2.0

if(w!=undefined&&isOpen()){

w.close();

}

w=window.open("",winName,features);

w.location.replace(theURL);

}

functionisOpen()

{

try

{

w.document;

returntrue;

}

catch(ex)

{}

returnfalse;

}

//-->

</script>

</head>

<body>

<ahref="javascript:;"onClick="MM_openBrWindow(show.asp’,’’,’scrollbars=yes,resizable=yes,width=570,height=500’)">title</a>

</body>

</html>

【用javascript父窗口控制只弹出一个子窗口】相关文章:

JavaScript实现弹出模态窗体并接受传值的方法

javascript自定义右键弹出菜单实现方法

删除javascript所创建子节点的方法

javascript实现删除前弹出确认框

javascript改变和控制显示的图片大小

JavaScript实现的MD5算法完整实例

用javascript制作放大镜放大图片

浅谈Javascript线程及定时机制

javascript实现控制的多级下拉菜单

用JavaScript实现对话框的教程

精品推荐
分类导航