JavaScript(Iframe、window.open、window.showModalD...

本文详细介绍了如何使用JavaScript实现不同浏览器环境下IFrame与父窗口之间的数据交互,包括刷新、取值、赋值等操作;同时涵盖了Window.open弹窗的多种交互方式及模态窗口的应用技巧。

一、Iframe

//父对象得到子窗口的值

//ObjectID是窗口标识,ContentID是元素ID

function GetValue(ObjectID,ContentID)

{

       var IsIE = (navigator.appName == 'Microsoft Internet Explorer')

                     if(IsIE)

                     {//如果是IE         

                            alert(document.frames(ObjectID).document.getElementById(ContentID).innerHTML);                              

                     }

                     else

                     {//如果是FF

                             alert(document.getElementById(ObjectID).contentDocument.getElementById(ContentID).innerHTML);

                                   //FF下不支持innerText; 下面是解决方法                     

                                   //if(document.all){

                                   // alert(document.getElementById('div1').innerText);

                                   //} else{

                                   //  alert(document.getElementById('div1').textContent);

                                   //}

                     }    

}

 

//父对象向子窗口赋值

//ObjectID是窗口标识,ContentID是元素ID

function SetValue(ObjectID,ContentID)

{

var IsIE = (navigator.appName == 'Microsoft Internet Explorer')

              if(IsIE)

              {//如果是IE         

                     document.frames(ObjectID).document.getElementById(ContentID).innerHTML="我是IE下通过父窗口赋值过来的";                            

              }

              else

              {//如果是FF

                      document.getElementById(ObjectID).contentDocument.getElementById(ContentID).innerHTML="我是FF下通过父窗口赋值过来的";                  

              }    

      1.父窗口对子窗口操作

 

刷新:

      document.getElementById("IframeID").src=document.getElementById("IframeID").src+"?_="+Math.random();

上面这种方法有时需要对“src”属性处理一下。

 

取值:

//父窗口取子窗口的值

       GetValue("Iframe1","IframeDiv");

 

赋值:

//父窗口设置窗口元素的值;

       SetValue("Iframe1","IframeDiv");      

 

   2.子窗口操作父窗口

 

              刷新:

           (1)window.parent.location.href=window.parent.location.href;  

           (2)window.parent.location.reload();

              (3)、大家可以补充

 

    取值:

alert(window.parent.document.getElementById("IframeDiv").innerHTML);    

 

赋值:

window.parent.document.getElementById("IframeDiv").innerHTML="我是从子窗口IFRAME传过来的值";

 

关闭:

window.parent.opener=null;//如果不加这句,会提示关闭询问窗口;

window.parent.close();

二、window.open

1.父窗口对子窗口操作

打开:

var win=null;

win=window.open("Open.html","win","width=200,height=200");

 

最大化:

//窗口最大化

function SonMaximize()

{

       if(win&&win.open&&!win.closed)

       {

              win.moveTo(-4,-4);

              win.resizeTo(screen.availWidth+8,screen.availHeight+8);

       }else{

              alert('还没有打开窗口或已经关闭');

       }

}

 

最小化:

//窗口最小化

function SonMinimize()

{

       if(win&&win.open&&!win.closed)

       {

              win.resizeTo(0,0);

              win.moveTo(0,window.screen.width);

       }else{

       alert('还没有打开窗口或已经关闭');

       }    

}

 

 

 

关闭:

//关闭窗口

function CloseSon()

{

       if(win&&win.open&&!win.closed)

       {

              win.opener=null;

              win.close()

       }else{

              alert('还没有打开窗口或已关闭') ;

       }

}

 

刷新:

//刷新

function RefreshSon()

{

       if(win&&win.open&&!win.closed)

       {

              win.location.reload();

              win.focus();

       }else{

              alert('窗口还没有打开或已关闭');

       }

}

 

查看窗口大小:

function ViewSonSize()

{

       if(win&&win.open&&!win.closed)

       {

              alert(win.document.body.clientWidth+'*'+win.document.body.clientHeight);

              win.focus();

       }else

       {

              alert(' 还没有打开窗口或者已关闭');

       }    

}

 

取值:

alert(window.document.getElementById("OpenDiv").innerHTML);

 

赋值:

win.document.getElementById("OpenDiv").innerHTML="我是从父窗口中传过来的值";

 

2.子窗口操作窗口

 

刷新:

window.opener.location.reload();

       //下面这种方法也可以

       //window.parent.location.href=window.parent.location.href;

 

 

 

 

 

关闭本窗口:

//关闭本窗口

function CloseWindow()

{     //window.opener.opener=null;

       window.close();

}

 

关闭父窗口:

//关闭父窗口

function CloseParent()

{     //火狐下不起作用,如果要想起作用。用下面的方法

    //firefox,在地址栏输入about:config      

       //找到dom.allow_scripts_to_close_windows这项并改为true

              var IsIE = (navigator.appName == 'Microsoft Internet Explorer')

              if(IsIE){//如果是IE            

                     window.opener.opener=null;

                     window.opener.close();

                     window.close();     

              }else{

                     alert("火狐不能直接关闭;需要以下设置1.firefox,在地址栏输入about:config;2.找到dom.allow_scripts_to_close_windows这项并改为true");

              }

      

}

 

取值:

alert(window.opener.document.getElementById("OpenDiv").innerHTML);     

 

赋值:

window.opener.document.getElementById("OpenDiv").innerHTML="我是从子窗口Open传过来的值";           

 

三、模态窗口篇

1.父窗口操作子窗口

父窗口JS代码:

var parValue="现在显示了父窗口中的变量值";

var hao="郝建卫";

function ShowDailog(PageHref,Title,Height,Width)

{

       //--------------left位置

       //screen.availHeight声明了显示浏览器的屏幕的可用宽度

       var dleft =(screen.availHeight-Height)/2;

       //--------------top位置

       var dtop =(screen.availWidth-Width)/2;

       //---------------

 

Var sRet = window.showModalDialog(PageHref,window,Title,"scrollbars=yes;resizable=no;help=no;status=no;center=yes;dialogTop=25;dialogLeft="+ dleft +";dialogTop="+ dtop +";dialogHeight="+Height+"px;dialogWidth="+Width+"px;");

       //--------return

       if (sRet =="refresh")//这种是利用返回值来刷新父页面

       {

              window.Test="true";

              window.location.reload();            

              alert(window.Test);

       }

}

function test()

{

       alert("模态窗口成功调用父窗口的方法");

}

2.模态窗口操作父窗口

var parentWin=window.dialogArguments; 

 

刷新:

       parentWin.location.reload(); 

 

取值:

alert(parentWin.document.getElementById("ShowModalDialogDiv").innerHTML)   //获取父窗口中的对象

 alert("我是从父窗口中得到的变量>>>"+parentWin.parValue);       //获取父窗口中的变量

 

调用父窗口JS方法:

parentWin.test();    //调用父窗口中的方法

 

赋值:

parentWin.document.getElementById("ShowModalDialogDiv").innerHTML="我是从子窗口ShowModalDialog传过来的值";      

 

关闭本窗口:

//关闭本窗口

function CloseWindow()

{

       window.parent.close();

}

 

关闭父窗口:

//关闭父窗口

function CloseModal()

{    

       var IsIE = (navigator.appName == 'Microsoft Internet Explorer')

              if(IsIE){//如果是IE            

                     window.parent.parent.close();

                     //parentWin.opener=null;如果把上面的换成这行,不能关闭父窗口,

                     parentWin.close();

                     //window.parent.parent.parent.parent.close();这个只能关闭模态窗口本身目前只在IE6下测试

              }else{

                     alert("火狐不能直接关闭;需要以下设置1.firefox,在地址栏输入about:config;2.找到dom.allow_scripts_to_close_windows这项并改为true");

              }    

}

转载于:https://my.oschina.net/u/235267/blog/49493

在使用 `iframe` 或 `window.open` 时,文件无法自动下载的原因通常与浏览器的安全策略、MIME 类型设置以及服务器响应头配置有关。以下是几种解决方法,可帮助实现文件的自动下载: ### 使用隐藏的 `<a>` 标签触发下载 通过创建一个带有 `download` 属性的 `<a>` 元素,并模拟点击操作,可以绕过浏览器对 `window.open` 和 `iframe` 的限制,实现文件的自动下载。示例代码如下: ```javascript function downloadFile(url, filename) { const a = document.createElement('a'); a.href = url; a.download = filename; document.body.appendChild(a); a.click(); document.body.removeChild(a); } ``` 此方法适用于大多数现代浏览器,且不依赖服务器端的响应头设置[^2]。 ### 使用 `fetch` 和 `Blob` 实现下载 通过 `fetch` 获取文件内容并将其转换为 `Blob` 对象,然后使用 `URL.createObjectURL` 创建临时链接,结合 `<a>` 标签进行下载。这种方式可以避免跨域问题,并且适用于需要处理二进制流的场景。示例代码如下: ```javascript async function downloadFileWithFetch(url, filename) { const response = await fetch(url); const blob = await response.blob(); const downloadUrl = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = downloadUrl; a.download = filename; document.body.appendChild(a); a.click(); window.URL.revokeObjectURL(downloadUrl); document.body.removeChild(a); } ``` 该方法适用于需要控制下载过程的场景,尤其是当文件需要通过认证或动态生成时[^2]。 ### 服务器端设置 `Content-Disposition: attachment` 如果可以修改服务器配置,建议在响应头中添加 `Content-Disposition: attachment`,这样浏览器会直接触发下载行为,而不是尝试在浏览器中预览文件。例如,在 Apache 或 Nginx 中可以通过以下方式设置: ```apache # Apache 配置示例 <FilesMatch "\.pdf$"> Header set Content-Disposition "attachment" </FilesMatch> ``` ```nginx # Nginx 配置示例 location ~ \.pdf$ { add_header Content-Disposition "attachment"; } ``` 此方法适用于静态文件下载,且能确保所有浏览器行为一致[^1]。 ### 使用 `iframe` 结合 `postMessage` 进行跨域通信 如果遇到跨域问题,可以通过 `postMessage` 实现父页面与 `iframe` 之间的通信,确保在正确的上下文中触发下载逻辑。例如: ```javascript window.addEventListener('message', function(e) { if (e.origin !== 'https://yourdomain.com') return; const url = e.data.url; const a = document.createElement('a'); a.href = url; a.download = 'file.pdf'; document.body.appendChild(a); a.click(); document.body.removeChild(a); }); ``` 同时,在 `iframe` 中发送消息: ```javascript window.parent.postMessage({ url: 'https://example.com/file.pdf' }, 'https://yourdomain.com'); ``` 此方法适用于嵌入在第三方页面中的 `iframe` 场景,能够有效解决跨域问题[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值