Heim > Web-Frontend > js-Tutorial > 用javascript父窗口控制只弹出一个子窗口_javascript技巧

用javascript父窗口控制只弹出一个子窗口_javascript技巧

WBOY
Freigeben: 2016-05-16 19:15:08
Original
1281 Leute haben es durchsucht

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

<script> <br>window.open("http://www.yeboss.com.cn","yeboss","width=300,height=400") <br></script> 
--------------------------------------------------------------- 

cookie 
--------------------------------------------------------------- 


 

<script> <br><br><!-- Begin <br>var expDays = 1; // number of days the cookie should last <br><br>var page = "only-popup-once.html"; <br>var windowprops = "width=300,height=200,location=no,toolbar=no,menubar=no,scrollbars=no,resizable=yes"; <br><br>function GetCookie (name) { <br>var arg = name + "="; <br>var alen = arg.length; <br>var clen = document.cookie.length; <br>var i = 0; <br>while (i < clen) { <br>var j = i + alen; <br>if (document.cookie.substring(i, j) == arg) <br>return getCookieVal (j); <br>i = document.cookie.indexOf(" ", i) + 1; <br>if (i == 0) break; <br>} <br>return null; <br>} <br>function SetCookie (name, value) { <br>var argv = SetCookie.arguments; <br>var argc = SetCookie.arguments.length; <br>var expires = (argc > 2) ? argv[2] : null; <br>var path = (argc > 3) ? argv[3] : null; <br>var domain = (argc > 4) ? argv[4] : null; <br>var secure = (argc > 5) ? argv[5] : false; <br>document.cookie = name + "=" + escape (value) + <br>((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + <br>((path == null) ? "" : ("; path=" + path)) + <br>((domain == null) ? "" : ("; domain=" + domain)) + <br>((secure == true) ? "; secure" : ""); <br>} <br>function DeleteCookie (name) { <br>var exp = new Date(); <br>exp.setTime (exp.getTime() - 1); <br>var cval = GetCookie (name); <br>document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString(); <br>} <br>var exp = new Date(); <br>exp.setTime(exp.getTime() + (expDays*24*60*60*1000)); <br>function amt(){ <br>var count = GetCookie(&#146;count&#146;) <br>if(count == null) { <br>SetCookie(&#146;count&#146;,&#146;1&#146;) <br>return 1 <br>} <br>else { <br>var newcount = parseInt(count) + 1; <br>DeleteCookie(&#146;count&#146;) <br>SetCookie(&#146;count&#146;,newcount,exp) <br>return count <br> } <br>} <br>function getCookieVal(offset) { <br>var endstr = document.cookie.indexOf (";", offset); <br>if (endstr == -1) <br>endstr = document.cookie.length; <br>return unescape(document.cookie.substring(offset, endstr)); <br>} <br><br>function checkCount() { <br>var count = GetCookie(&#146;count&#146;); <br>if (count == null) { <br>count=1; <br>SetCookie(&#146;count&#146;, count, exp); <br><br>window.open(page, "", windowprops); <br><br>} <br>else { <br>count++; <br>SetCookie(&#146;count&#146;, count, exp); <br> } <br>} <br>// End --> <br></script> 


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

<script> <br><!-- Copyright fenggang at 2007/3/23 <br>var w=null; <br>function MM_openBrWindow(theURL,winName,features) { //v2.0 <br> if(w!=undefined&&isOpen()){ <br> w.close(); <br> } <br> w=window.open("",winName,features); <br> w.location.replace(theURL); <br>} <br>function isOpen() <br>{ <br> try <br> { <br> w.document; <br> return true; <br> } <br> catch(ex) <br> {} <br><br> return false; <br>} <br>//--> <br></script>
这个MM_openBrWindow()函数名是在Dreamweaver中常用的弹出窗口函数名,将这段代码放进页面里
然后再做弹出窗口就只能探出一个窗口了,以上是精髓部分,下面是完整的一个测试页的代码,试试看吧。

nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



无标题文档
<script> <br><!-- Copyright fenggang at 2007/3/23 <br>var w=null; <br>function MM_openBrWindow(theURL,winName,features) { //v2.0 <br> if(w!=undefined&&isOpen()){ <br> w.close(); <br> } <br> w=window.open("",winName,features); <br> w.location.replace(theURL); <br>} <br>function isOpen() <br>{ <br> try <br> { <br> w.document; <br> return true; <br> } <br> catch(ex) <br> {} <br><br> return false; <br>} <br>//--> <br></script>


title

Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage