window.open代替window.showModalDialog(非标准的BOM方法)
来源:互联网 发布:linux终端装中文输入法 编辑:程序博客网 时间:2024/05/01 16:32
1, 最基本的弹出窗口代码
window.open('page.html');
2, 经过设置后的弹出窗口
window.open('page.html', 'newwindow', 'height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no') //该句写成一行代码 参数解释: window.open 弹出新窗口的命令; 'page.html' 弹出窗口的文件名; 'newwindow' 弹出窗口的名字(不是文件名),非必须,可用空''代替; height=100 窗口高度; width=400 窗口宽度; top=0 窗口距离屏幕上方的象素值; left=0 窗口距离屏幕左侧的象素值; toolbar=no 是否显示工具栏,yes为显示; menubar,scrollbars 表示菜单栏和滚动栏。 resizable=no 是否允许改变窗口大小,yes为允许; location=no 是否显示地址栏,yes为允许; status=no 是否显示状态栏内的信息(通常是文件已经打开),yes为允许;
3, 用函数控制弹出窗口
下面是一段完整的代码<html> <head> <script LANGUAGE="JavaScript"> <!-- function openwin() { window.open ("page.html", "newwindow", "height=100, width=400, toolbar =no, menubar=no, scrollbars=no, resizable=no, location=no, status=no") //写成一行 } //--> </script> </head> <body onload="openwin()"> 任意的页面内容... </body> </html>解释:这里定义了一个函数openwin(), 函数内容就是打开一个窗口。在调用它之前没有任何用途。怎么调用呢?
方法一:<body onload="openwin()"> 浏览器读页面时弹出窗口;
方法二:<body onunload="openwin()"> 浏览器离开页面时弹出窗口;
方法三:用一个连接调用:
<a href="#" onclick="openwin()"> 打开一个窗口</a>
注意:使用的"#"是虚连接。
方法四:用一个按扭调用:
<input type="button" onclick="openwin()" value="打开窗口" />
4, 弹出两个窗口
对代码稍微改动如下:<script LANGUAGE="JavaScript"> <!-- function openwin() { window.open ("page.html", "newwindow", "height=100, width=100, top=0, left=0,toolbar=no, menubar=no, scrollbars=no, resizable=no, location=n o, status=no")//写成一行 window.open ("page2.html", "newwindow2", "height=100, width=100, top=100, left=100,toolbar=no, menubar=no, scrollbars=no, resizable=no, loca tion=no, status=no")//写成一行 } //--> </script>为避免弹出的2个窗口覆盖,用top和left控制一下弹出的位置不要相互覆盖即可。最后用上面的说过的四种方法调用即可。
注意:2个窗口的name(newwindow与 newwindow2)不要相同,或者干脆全部为空。
5, 主窗口打开文件1.htm,同时弹出小窗口page.html
如下代码加入主窗口<head>区:function openwin() { window.open("page.html","","width=200,height=200") }加入body区:
<a href="1.htm" onclick="openwin()">open</a>即可。
6, 弹出的窗口之定时关闭控制
将一小段代码加入弹出的页面(注意是加入page.html的HTML中,可不是主页面中,否则......),让它在10秒后自动关闭是不是更酷了?function closeit()
{
setTimeout("selft.close()", 10000) //毫秒
}
然后,在body 中添加:<body onload="closeit()">即可。
7, 在弹出窗口中加上一个关闭按扭
<input type="button" value="关闭" onclick="window.close()">8, 内包含的弹出窗口---一个页面两个窗口
上面的例子都包含两个窗口,一个是主窗口,另一个是弹出的小窗口。通过下面的例子,你可以在一个页面内完成上面的效果。<html> <head> <SCRIPT LANGUAGE="JavaScript"> function openwin() { OpenWindow=window.open("", "newwin", "height=250, width=250,toolbar=no ,scrollbars="+scroll+",menubar=no"); //写成一行 OpenWindow.document.write("<TITLE>例子</TITLE>") OpenWindow.document.write("<BODY BGCOLOR=#ffffff>") OpenWindow.document.write("<h1>Hello!</h1>") OpenWindow.document.write("New window opened!") OpenWindow.document.write("</BODY>") OpenWindow.document.write("</HTML>") OpenWindow.document.close() } </SCRIPT> </head> <body> <a href="#" onclick="openwin()">打开一个窗口</a> <input type="button" onclick="openwin()" value="打开窗口"> </body> </html>OpenWindow.document.write()里面的代码不就是标准的HTML吗?只要按照 格式写更多的行即可。千万注意多一个标签或少一个标签就会出现错误。记得用 OpenWindow.document.close()结束啊。
9, 终极应用---弹出的窗口这Cookie控制
回想一下,上面的弹出窗口虽然酷,但是有一点小毛病,比如你将上面的脚本放在一个需要频繁经过的页面里(例如首页),那么每次刷新这个页面,窗口都会弹出一次,是不是非常烦人?有解决的办法吗?我们使用cookie来控制即可。
首先,将如下代码加入主页面的Html的head区:
function openwin(){ window.open("page.html","","width=200,height=200") } function get_cookie(Name) { var search=Name+"="; var returnvalue=""; if(document.cookie.length>0){ if (offset != -1) { offset += search.length end = document.cookie.indexOf(";", offset); if (end == -1) end = document.cookie.length; returnvalue=unescape(document.cookie.substring(offset, end)); } } return returnvalue; } function ladpopup() { if(get_cookie('popped=yes')) { openwin() document.cookie="popped=yes"; } }最后,用<body onload="loadpopup()">
10, 弹出窗口居中显示
var openUrl = "";//弹出窗口的urlvar iWidth=800; //弹出窗口的宽度;var iHeight=600; //弹出窗口的高度;var iTop = (window.screen.availHeight-30-iHeight)/2; //获得窗口的垂直位置;var iLeft = (window.screen.availWidth-10-iWidth)/2; //获得窗口的水平位置;window.open(openUrl,"","height="+iHeight+", width="+iWidth+", top="+iTop+", left="+iLeft);
0 0
- window.open代替window.showModalDialog(非标准的BOM方法)
- window.open()代替window.showModalDialog()的方法
- 完全可以用window.open()代替window.showModalDialog()的方法
- window.open()代替window.showModalDialog()
- 真正的用window.open()代替window.showModalDialog()
- 真正的用window.open()代替window.showModalDialog()
- 用window.open代替window.showModalDialog和window.showModelessDialog
- window.showModalDialog(),与window.open()刷新上一个窗口的方法
- window.showModalDialog以及window.open的区别
- window.open、window.showModalDialog和window.showModelessDialog 的区别
- window.open、window.showModalDialog和window.showModelessDialog 的区别
- window.open、window.showModalDialog和window.showModelessDialog 的区别
- window.open,window.showModalDialog用法
- window.showModalDialog() and window.open()
- window.open()变成window.showmodaldialog()
- window.open与window.showModalDialog
- window.showModalDialog Vs window.open
- window.open与showModalDialog
- JVM参数设置PermSize大于PermMaxSize,结果会怎么样?
- Android Studio 添加动态库
- TakePhoto-轻量级Android照片处理框架
- Android Eclipse安装Ant插件
- MYSQL向日期添加指定的时间间隔
- window.open代替window.showModalDialog(非标准的BOM方法)
- IDM退出机制研究
- Java创建线程的两个方法
- 【Oh-Hard】瘦人增肌是不是要先吃胖?怎么练怎么吃?
- [LeetCode][11]Container With Most Water解析 时间复杂度为O(n) -Java实现
- iMindMap是怎样在学术研究上做贡献的
- CentOS 上通过 yum 快速安装最新版 Transmission
- PCA求解思路
- eclipse, Log4j配置(真心的详细~)