问题关闭或重新加载“弹出窗口”窗口

时间:2012-08-24 03:53:02

标签: javascript popup refresh reload

我无法找到关闭或重新加载下面代码生成的弹出框的方法。我想要避免的是,如果用户没有手动关闭窗口然后选择另一个“提示”,则新提示将简单地附加到现有文本。期望的结果是;

  1. 当窗口丢失焦点时窗口关闭,从而重置窗口。
  2. 我可以在粘贴新提示时“刷新”窗口。
  3. 我做了大量的研究,并尝试了许多不同的方法,但没有用。

    请记住,我正在处理一个紧张的政府环境,他们对安装第三方工具(如jQuery或类似工具)非常挑剔,所以我真的很难在这里使用基础工具,直到我能说服他们。此外,对于MOST部分,部门中的大多数用户将使用IE8或更高版本,但(作为较低优先级)我宁愿开发一种不限于特定浏览器的解决方案。

    也许有人可以建议一种更好的方式来做我正在做的事情,还是那个要求太多? ;-)提前致谢。

    function PromptTip(title, message)
    {
    var HTMLTitle = "<p style='text-align:center'><b>" + title + "</b></p>"
    var HTMLMsg = "<p style='text-align:center'>" + message + "</p>"
    win = window.open("","Tip","width=320,height=210,scrollbars=no,resizable=no,titlebar=no");
    win.moveTo(screen.width/2-160,screen.height/2-105)
    win.document.writeln("<html><head><title>Prompt Tip</title></head><body onfocusout='javascript:close()'>");
    win.document.writeln(HTMLTitle);
    win.document.writeln(HTMLMsg);
    win.document.writeln("<p style='text-align:center'><a href='javascript:close()'>close</a></p></body></html>");
    }
    

2 个答案:

答案 0 :(得分:1)

我强烈建议不要使用window.open和window.document.writeln。取代window.open我建议动态创建标记(或显示隐藏的标记片段)并使用绝对定位,就像所有现代对话框/模态JS库一样。取代win.document.writeln我建议使用document.createElement创建DOM并使用Node.appendChild(或任意数量的DOM插入方法)或使用标记模板方法进行插入。

也就是说,每次单击工具提示时,您只需修改代码即可替换iframe主体的整个innerHTML。

示例:http://jsfiddle.net/ruJqj/

function PromptTip(title, message) {
    var HTMLTitle = "<p style='text-align:center'><b>" + title + "</b></p>"
    var HTMLMsg = "<p style='text-align:center'>" + message + "</p>"
    win = window.open("", "Tip", "width=320,height=210,scrollbars=no,resizable=no,titlebar=no");
    win.moveTo(screen.width / 2 - 160, screen.height / 2 - 105)

    win.document.body.innerHTML = HTMLTitle + HTMLMsg + "<p style='text-align:center'><a href='javascript:close()'>close</a></p>";
}​

答案 1 :(得分:0)

如果我对你的问题进行了解,可能会有所帮助,你可以改变这一行:

    win.document.writeln("<html><head><title>Prompt Tip</title></head><body onfocusout='javascript:close()'>");

对于这个:

    win.document.writeln("<html><head><title>Prompt Tip</title></head><body onBlur='javascript:close()'>");

您需要使用此事件:onBlur ='javascript:close()'

*我只是在IE 9中测试它并且它工作正常