| <!-- File: NewsEdit.html
(http://news.mycompany.com/NewsEdit.html) --> <html> <head> <title>The Content Editing Interface</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <script language="JavaScript"> <!-- /* OpenWin 用来在一个弹出窗口中显示 ImgUpload.html 的内容*/ function OpenWin(){//Open window url='http://news.mycompany.com/upload/ImgUpload.html'; newwindow = window.open(url,"ImgUpload","height=135,width=300"); if (!newwindow.opener) newwindow.opener=self; } --> </script> </head> <body> <h2>Edit your content here</h2> <!-- 调用后台应用 newsedit 来保存新闻内容 --> <form action="http://news.mycompany.com/newsedit" method="post" name="addnews"> <!-- 新闻标题 --> Title:<input type="text" name="title"><br> <!-- 新闻作者 --> Author:<input type="text" name="author"><br> The content <br> <!-- 新闻内容 --> <textarea name="contentBody" cols="100" rows="10"></textarea> <br> <!-- 点击连接打开上传图片的小窗口 --> <a href="JavaScript:OpenWin()">Upload Image File</a> <br> <!-- UserImg 用来预览上传成功后的图片文件 --> <img name="UserImg" style="width: 100px; height: 100px;" src="" border="1"> <br><br> <input type="submit" name="SaveContent" value="Submit"> <input type="reset" name="ClearContent" value="Reset"> </form> </html> |
| <!-- File: ImgUpload.html
(http://news.mycompany.com/upload/ImgUpload.html) --> <html> <head> <title>Imgage Upload Interface</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head> <body> <h2>Image Upload</h2> <!-- 调用后台应用来处理上传的图片 --> <form action="http://news.mycompany.com/upload/imgupload" method="post" enctype="multipart/form-data" name="upload"> <!-- 由用户选择本地文件 --> <input type="file" name="imgfile"> <input type="submit" name="Submit" value="Upload"> </form> </html> |
| <html> <head> <title>File Upload Successfully</title> </head> <body> <h3>File Uploaded Successfully!</h3> <script language="JavaScript"> <!-- 获取主窗口的句柄 --> parwin=self.opener; <!-- 获取对 img 元素的引用,并用上传文件的 url 为 img 元素的 src 属性赋值,这样在客户端就可以预览了 --> <!-- 为了简化问题,我们将对 img 元素的引用直接写在程序中 --> parwin.addnews.UserImg.src="http://news.mycompany.com/img/2003_07/06/1057478464859.gif"; </script> </body> </html> |
| 其实,你可以直接把实验一中 imgupload 返回的内容另存为一个文件并部署到 img.mycompany.com,在 NewsEdit.html 中调用 window.open() 方法直接引用这个文件就可以进行测试了。 |
| 利用其他脚本,如 vbscript 或 jscript 实现这种跨域的数据交换其原理与用 JavaScript 是一样的,大家可以参考相关资料来实现。 |
| 注意 Netscape Navigator 、Mozilla 和 Opera 浏览器是没有与之对应的方法的,也就是说除了 MSIE 之外其他几大浏览器都不支持用 showModalDialog 或 showModelessDialog 显示对话框,如果你感兴趣的话这里有一篇文章能够教你如何通过其他方式来模拟一个模态对话框,详见 Simulating Modal Dialog Windows |
| <html> <head> <title>show modal dialog</title> <script> <!-- //document.domain = "mycompany.com"; <!-- 打开一个模态对话框,显示 url 所代表的资源 --> function openDialog(url) { <!-- 向对话框传递参数 --> var args = new Object(); args.content = "Can you hear me?"; var rv = window.showModalDialog(url, args); <!-- 显示对话框所返回的结果 --> if (rv) { alert("dialog returns :" + rv); } else { alert("dialog returns nothing"); } } --> </script> </head> <body> <!-- 引用 b.mycompany.com 中的资源 --> <a href="#" onclick="openDialog('http://b.mycompany.com/remotedialog.html');return false;"> I will Open a remote dialog from news.soufun.com </a> <br> <!-- 引用本地的资源 --> <a href="#" onclick="openDialog('./invokebyhouse.html');return false;"> I will Open a local dialog </a> </body> </html> |
| <html> <head> <title>a remote dialog</title> <script> <!-- //document.domain = "mycompany.com"; onload = function() { var args = window.dialogArguments; alert("You send me: " + args.content); btnCan.onclick = function() { window.returnValue = "Yes I do, I hear you from " + document.domain; close(); } } --> </script> </head> <body> Im here, Im a dialog <br> I will return something to the main window<br> <input id="btnCan" type="button" style="text-align:center;" value="Close"> </body> </html> |
|
如果我的实验中存在某些漏洞,或者在你的实验中对话框读取到了从 main.html 传递过来的参数,有劳你通过 Email告知我,谢谢! 注(2004-12-28):您在进行测试的时候可能会得到不同的测试结果,因为随着IE 的更新或者补丁的作用,这种跨域的数据交换行为可能会被调整。例如笔者在加入这段注解的时候上面的第1条就已经不再成立了——main.html 并不是总能接收到从隶属于b.mycompany.com 的对话框返回的数据,只有两者都把document.domain 属性设置为mycompany.com 后main.html 才能接收到对话框返回的结果。见相关讨论:about showModalDialog |