用javascript父窗口控制只彈出一個子窗口
更新時間:2007年04月10日 00:00:00 作者:
近來網(wǎng)上經(jīng)常流傳一些關(guān)于如何在父窗口控制只彈出一個子窗口 問題,我查看了一些,大部分都是只能解決部分或者很麻煩,還不算完美。比如有人是實現(xiàn)的這樣,使用的是Cookie,以下是例子。
主頁上做了一個彈出窗口,如何使其只彈出一次,返回主頁時不再彈出了。
---------------------------------------------------------------
<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 should 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()">
這個是網(wǎng)上隨便摘的一個例子,是不是感覺很多也很難看懂呢,下面我給大家說一種新方法,保證好看好使。
<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.document;
return true;
}
catch(ex)
{}
return false;
}
//-->
</script>
這個MM_openBrWindow()函數(shù)名是在Dreamweaver中常用的彈出窗口函數(shù)名,將這段代碼放進(jìn)頁面里
然后再做彈出窗口就只能探出一個窗口了,以上是精髓部分,下面是完整的一個測試頁的代碼,試試看吧。
<%@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>無標(biāo)題文檔</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.document;
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>
主頁上做了一個彈出窗口,如何使其只彈出一次,返回主頁時不再彈出了。
---------------------------------------------------------------
<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 should 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()">
這個是網(wǎng)上隨便摘的一個例子,是不是感覺很多也很難看懂呢,下面我給大家說一種新方法,保證好看好使。
<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.document;
return true;
}
catch(ex)
{}
return false;
}
//-->
</script>
這個MM_openBrWindow()函數(shù)名是在Dreamweaver中常用的彈出窗口函數(shù)名,將這段代碼放進(jìn)頁面里
然后再做彈出窗口就只能探出一個窗口了,以上是精髓部分,下面是完整的一個測試頁的代碼,試試看吧。
<%@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>無標(biāo)題文檔</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.document;
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>
您可能感興趣的文章:
- jquery、js調(diào)用iframe父窗口與子窗口元素的方法整理
- js 父窗口控制子窗口的行為-打開,關(guān)閉,重定位,回復(fù)
- js操作模態(tài)窗口及父子窗口間相互傳值示例
- JavaScript實現(xiàn)彈出子窗口并傳值給父窗口
- JavaScript子窗口調(diào)用父窗口變量和函數(shù)的方法
- JavaScript新窗口與子窗口傳值詳解
- js創(chuàng)建子窗口并且回傳值示例代碼
- JS獲取子窗口中返回的數(shù)據(jù)實現(xiàn)方法
- jquery 子窗口操作父窗口的代碼
- iframe里面的元素觸發(fā)父窗口元素事件的jquery代碼
- JQUERY 獲取IFrame中對象及獲取其父窗口中對象示例
- 解析jquery獲取父窗口的元素
- JS與jQuery實現(xiàn)子窗口獲取父窗口元素值的方法
相關(guān)文章
值得學(xué)習(xí)的JavaScript調(diào)試技巧分享
這篇文章主要給大家介紹了一些JavaScript調(diào)試技巧,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06ES6?關(guān)鍵字?let?和?ES5?及關(guān)鍵字?var?的區(qū)別解析
var可以穿透控制語句、條件語句這樣的作用域,導(dǎo)致變量沖突經(jīng)常發(fā)生,這篇文章主要介紹了ES6?關(guān)鍵字?let?和?ES5?及關(guān)鍵字?var?的區(qū)別,需要的朋友可以參考下2022-09-09javascript實現(xiàn)C語言經(jīng)典程序題
這篇文章主要介紹了javascript實現(xiàn)C語言經(jīng)典程序題的解題思路,感興趣的小伙伴們可以參考一下2015-11-11JS自動跳轉(zhuǎn)手機(jī)移動網(wǎng)頁的實現(xiàn)方法
本文主要介紹了JS自動跳轉(zhuǎn)手機(jī)移動網(wǎng)頁的實現(xiàn)方法,可以通過檢查 navigator.userAgent 屬性來識別用戶代理字符串中包含的設(shè)備信息,下面就詳細(xì)的來介紹一下具體用法,感興趣的可以了解一下2024-03-03Bootstrap打造一個左側(cè)折疊菜單的系統(tǒng)模板(二)
這篇文章主要介紹了Bootstrap打造一個左側(cè)折疊菜單的系統(tǒng)模板(二)的相關(guān)資料,需要的朋友可以參考下2016-05-05JavaScript原生編寫《飛機(jī)大戰(zhàn)坦克》游戲完整實例
飛機(jī)大戰(zhàn)坦克是一款小游戲,相信很多朋友都有玩過,由于最近在深入學(xué)習(xí)Javascript,所以想著用利用Javascript來實現(xiàn)這個游戲,下面這篇文章主要介紹了如何利用JavaScript原生編寫《飛機(jī)大戰(zhàn)坦克》游戲,需要的朋友可以參考下2017-01-01