te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Javascript: How to get a list of all open windows - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript: How to get a list of all open windows - Stack Overflow

programmeradmin3浏览0评论

Suppose you open a handful of windows with:

  window.open(url1,'win1');
  window.open(url2,'win2');
  window.open(url3,'win3');

(each window has a unique name)

And then you refresh the page.

The 3 popup windows are still open. Is there a way to list the names of all of the open windows and close them?

This is not a duplicate question.

In this question the browser is being refreshed, so you cannot simply use a global array to keep track of child windows.

This is not a duplicate question.

Suppose you open a handful of windows with:

  window.open(url1,'win1');
  window.open(url2,'win2');
  window.open(url3,'win3');

(each window has a unique name)

And then you refresh the page.

The 3 popup windows are still open. Is there a way to list the names of all of the open windows and close them?

This is not a duplicate question.

In this question the browser is being refreshed, so you cannot simply use a global array to keep track of child windows.

This is not a duplicate question.

Share Improve this question edited Jul 7, 2014 at 2:19 Brian McGinity asked Jul 7, 2014 at 1:36 Brian McGinityBrian McGinity 5,9356 gold badges39 silver badges46 bronze badges 7
  • No there is not, unless the URLs are from the same domain. Than there might be a possibility to municate between those windows, but I don't think you can programmatically close them, since you lost the reference to the windows. – Felix Kling Commented Jul 7, 2014 at 1:40
  • The URLs are in the same domain. – Brian McGinity Commented Jul 7, 2014 at 1:44
  • 1 Then they could write to localStorage, as suggested here. Maybe have the child windows listen to changes and have them close themselves. – Felix Kling Commented Jul 7, 2014 at 1:47
  • localStorage would work. I was hoping there might be a property in the window object which I've yet to find. – Brian McGinity Commented Jul 7, 2014 at 1:53
  • I don't think so. When you reloaded the page you are creating a new JS environment which has nothing to do with the previous environment, even if it was the same page. – Felix Kling Commented Jul 7, 2014 at 1:55
 |  Show 2 more ments

2 Answers 2

Reset to default 6

So the questions is closed, I'll post an answer based on the ments and research.

Firstly, to all who mented, thank you for helping.

Answer: There is not a built-in object which tracks opened windows and persists from page load to page load.

As Felix Kling pointed out, using localStorage is a possible work-around.

Try postMessage to municate between existing windows within the same domain. That's how i'm going to try and solve the same problem. See: https://developer.mozilla/en-US/docs/Web/API/Window/postMessage

index.htm

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>pop</title>
  </head>
  <body>

    <script>

var pops = [];

window.onmessage = function(e)
{

    // todo: check domain 
    // if( e.origin )
    var data;
    try
    {
        data = JSON.parse(e.data);
    }
    catch(e)
    {
        // fail silent...?
        return;
    }
    switch(data.event)
    {
    case "RestoreOpenWindow":
        onClosePopup(e.source.name);
    case "QueryOpenWindows":
        pops.push(e.source);
        updateLabel();
        break;
    }
};

window.onload = function()
{
    window.onClosePopup = onClosePopup;
    updateLabel();
};

window.onbeforeunload = function()
{
    for(var i = 0; i < pops.length; i++) pops[i].queryOpenPopups();
};

function onClosePopup(name)
{
    for(var i = pops.length - 1; i >= 0; i--)
        if(pops[i].name === name)
            { pops.splice(i, 1); break; }
    updateLabel();
};

function openPopup()
{
    pops.push(window.open("pop/popup.htm", "pop" + pops.length, ' '));
    updateLabel();
    setTimeout(function(){
        alert('Click ok to refresh...');
        location.href = location.href;
    }, 5000);
}

function updateLabel()
{
    document.getElementById("total").innerHTML = pops.length;
    var html = [];
    for(var i = 0; i < pops.length; i++)
        html.push(pops[i].name);
    document.getElementById("names").innerHTML = html.join("<br"+"/"+">");
}


    </script>
    <button onclick="openPopup()">open popup and refresh after 5 seconds (...allow em popups...)</button></br>
    <span>total: </span><span id="total"></span></br>
    <span id="names"></span></br>
  </body>
</html>

popup.htm

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>pop</title>
  </head>
  <body>

    <script>

window.queryOpenPopups = function()
{
    var count = 0;
    var hInterval = setInterval(function () {
        try
        {
            if(window.opener)
            {
                window.opener.postMessage(JSON.stringify({"event": "QueryOpenWindows", "name": window.name}), "*");
                clearInterval(hInterval);
            } else count++;
        }
        catch(e)
        {
            count++;
        }       
        if(count > 50)window.close();
    }, 100);
};

window.onbeforeunload = function(){
    window.opener.onClosePopup(window.name);
};

// restore link with opener on refresh
window.opener.postMessage(JSON.stringify({"event": "RestoreOpenWindow", "name": window.name}), "*");

window.onload=function(){ document.getElementById("name").innerHTML = window.name; };
    </script>       
    <span id="name"></span>
  </body>
</html>
发布评论

评论列表(0)

  1. 暂无评论