开发者

how to get browser window object using java script or jQuery

I would like to get window object of the already open window and I don't want to open a new window when expected window is closed.

I tried the following option:

var windowObj = window.open('','windowName', '');

However it opens a new window when expected window is not present/closed.

Please suggest me some开发者_开发问答thing to get the window object using JavaScript or jQuery.


You can't. You need to keep the references of opened windows when calling the window.open function. It is not possible to acquire a reference otherwise.

A solution would be to create a new window only when the previous one was closed, or reuse it otherwise. Something like :

var _childWin = null;

function getChildWin() {
   if (_childWin == null || _childWin.closed) {
       _childWin = window.open(...);
   }
   return _childWin;
}


How about just var windowObj = window; ? You don't even need to assign it to your own variable.


There are really two ways to get "window"

var thisWindow = window; // window script resides
var newOpenWindow = window.open(parameters for window here);// window opened

For the newOpenWindow, you can check if it exists prior to opening it (did you do the "open" already basically) by looking for a false value

if (newOpenWindow) // if true it is open

Other windows, you would not be able to detect from a script standpoint as they are out of scope of the script within a window/browser instance once they are opened.

There is also the "contained window" which is really a document within an iframe, which is really a different matter altogether.

EDIT: Illustrate window interaction

Create a child window named TestCallBack.html

Note jQuery in both windows.

Show some functional interaction between the windows (passing from child, the window and a jQuery object of the new windows document):

Child window layout:

<!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>
    <title>iamchild</title>
    <script src="JS/jQuery/jquery.js" type="text/javascript"></script>
    <script src="JS/TestCallBack.js" type="text/javascript"></script>
</head>
<body>
  Howdy
  <div id="achildy">HereIBe
    <div id="inchildy">
      I am childy text
    </div>
  </div>
</body>
</html>

text of the TestCallBack.js file:

var ct;
function childCallBack(passstuff)
{
    alert('ct:"' + ct + '" CHILD GOT:(' + passstuff + ")");
    return ct;
};
$(document).ready(function()
{
    ct = $("#achildy").text();
    window.opener.logNewWindow(window, $(document));
});

Javascript in parent window to open child window: (and function for child to call)

function logNewWindow(newWindow, JQnewWindowDoc)
{
    var mychildText = JQnewWindowDoc.text();//all the child doc text
    var innerChildText = $("#inchildy", JQnewWindowDoc).text();// one element text
    var gotback = newWindow.childCallBack("CHILD TEXT:" + mychildText + " INNER:" + innerChildText);
    alert("GOT:" + gotback); //child sent me this text from childCallBack
};

var AWindow = window.open("TestCallBack.html");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜