您的位置:首页 > Web前端 > JavaScript

JS实现子窗口传值到父窗口

2016-07-01 15:33 721 查看
以前很少用JS,通过这次学习MVC碰到不少问题,比如子窗口要传值给父窗口。

方法大致有两种,此处说当前使用的这种方法:

父窗口打开子窗口时,使用window.open方式打开,以下摘抄了两个使得子窗口居中的JS方法,第一个是子窗口屏幕居中,第二个是在上级页面中居中

1>

/*打开网页 window.open

/*url:          表示请求路径

/*windowname:   定义页名称

/*width:        宽度

/*height:       高度

---------------------------------------------------*/

function OpenWindowByScreen(url, title, w, h) {

    var iWidth = w;

    var iHeight = h;

    var iTop = (window.screen.availHeight-30-iHeight)/2;       //获得窗口的垂直位置;

    var iLeft = (window.screen.availWidth-10-iWidth)/2;           //获得窗口的水平位置;

    window.open(RootPath() + url, title, 'height=' + iHeight + ',,innerHeight=' + iHeight + ',width=' + iWidth + ',innerWidth=' + iWidth + ',top=' + iTop + ',left=' + iLeft + ', toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no, titlebar=yes,
alwaysRaised=yes');

}

2>

/*打开网页 window.open

/*url:          表示请求路径

/*windowname:   定义页名称

/*width:        宽度

/*height:       高度

---------------------------------------------------*/

function OpenWindow(url, title, w, h) {

    var width = w;

    var height = h;

    var left = ($(window).width() - width) / 2;

    var top = ($(window).height() - height) / 2;

    window.open(RootPath() + url, title, 'height=' + height + ', width=' + width + ', top=' + top + ', left=' + left + ', toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no, titlebar=yes, alwaysRaised=yes');

}

————————————————————————————————————————————————————————

子窗口传值给父窗口时:

window.opener.document.getElementById("Downloads").value += '新值';

此处的Downloads即为父窗口中的一个textarea元素

要求父窗口打开子窗口时须使用window.open方法才能使用window.opener回传值
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: