【问题标题】:How to check whether a Browser Window is Opened or not in JSF or Primefaces?如何在 JSF 或 Primefaces 中检查浏览器窗口是否打开?
【发布时间】:2014-04-15 06:38:19
【问题描述】:

我正在使用jsfprimefaces

我有一个表单,其中我有一个 StudentId 输入文本和一个命令链接以在新的 Pop-Up 中查看 Student Details 和一个 Get 要显示的命令按钮

我想在点击获取按钮时实现这一点。

  • 如果之前未打开弹出窗口,则不要打开弹出窗口。
  • 如果弹出窗口已打开,则使用新参数刷新该窗口。

这是我的代码:

查看页面:

...//js code is below ..

<h:outputText value="StudentId :" />
        <p:inputText value="#{myController.studentId}" >
            <pe:keyFilter mask="num"/>
        </p:inputText>
    <p:commandButton value="Get" actionListener="#{myController.getStudentMarksDetails(myController.studentId)}" >
     
<p:commandLink onclick="openWindow('http://www.google.com','pageName')" >
        <h:outputText value="Student Info "> 
</p:commandLink>

    ...//Displays Student Marks
    

我的控制器

public Student getStudentMarksDetails(Long studentId){
     //Some Code to Get the Student Marks
    
    // I want to Check whether a Pop-Up already exists or not, if pop-up is opened then refresh with this url
    RequestContext.getCurrentInstance().execute("updatePopUpWindow('http://stackoverflow.com','pageName');");
    
}

我的 JS

  var myWindow;
    //Opens in the Same window 
    function openWindow(url, name){
          myWindow = window.open(url, name,'status=no,toolbar=no,location=no,menubar=no,resizable,width=1008,height=690,scrollbars,left=100,top=50,').focus();
    }
     
    // To check whether a Pop-Up window is opened or not 
    //if the pop-up window with pageName is not Opened before then dont open a Pop-Up.
    //if the pop-up window is already opened, then refresh that window with new arguments.
    
    function updatePopUpWindow(url, name){
        if(myWindow!=null){
            if(!myWindow.closed){
                 myWindow.close();
            }
        }
     //Check the condition Here
        myWindow = window.open(url, name,'status=no,toolbar=no,location=no,menubar=no,resizable,width=1008,height=690,scrollbars,left=100,top=50,').focus();
        
    }

我试过放置这个条件但没有用:

if(myWindow.name == name){
                myWindow = window.open(url, name,'status=no,toolbar=no,location=no,menubar=no,resizable,width=1008,height=690,scrollbars,left=100,top=50,').focus();
            }

我推荐了很多links,但无法解决我需要的方式。

如何在JQuery 中实现此问题,因为JS 属性独立于浏览器

更新评论: 我无法使用 PF 对话框,因为我不知道该外部服务链接将如何以及何种类型的数据和视图来自该外部服务链接。认为我不是我传递来获取学生详细信息的 URL 的作者。

【问题讨论】:

  • 您确定要使用弹出窗口而不是 PF 模态对话框吗?
  • @perissf 我想打开一个新窗口,在 url 后面加上 studentId 来获取学生信息。我想打开一个新的浏览器窗口,而不是对话框!
  • @all 在我的情况下,学生信息详细信息由另一个外部项目提供,所以如果我必须打开一个新窗口并使用 StudentId 传递 URL 来调用另一个项目的外部服务!这不是我可以使用 PF 对话框的情况!
  • 仍然不明白为什么你不能按照我建议的方式使用它...你可以完全控制对话框,你可以完全控制你传递的 URL - 你还需要什么?
  • @Dodek 我想打开一个窗口,因为即使我们在父页面上执行任何其他操作,让 StudentDetailsInfo 窗口保持原样,我们也可以查看学生详细信息。

标签: java javascript jquery jsf primefaces


【解决方案1】:

我认为可能(也可能不会)是您可以使用 PF Dialog 的情况。取决于您要在其中显示的页面。我使用以下示例在对话框中显示外部页面的内容:

托管豆

@ManagedBean
@ViewScoped
public class Bean {
    private String myURL;

    public Bean() {
        myURL = "http://www.stuba.sk/english.html";
    }

    public void handleUpdateDialog(){
         myURL = "http://www.stuba.sk/english/news/news.html";
    }
    //getters and setters
    ...
}

xhtml

<h:form>
    <p:commandButton value="Show Dialog" oncomplete="dlg.show();" update="dlgId"/>
    <p:commandButton value="Update Dialog" actionListener="#{bean.handleUpdateDialog}" oncomplete="dlg.show();" update="dlgId"/>

    <p:dialog id="dlgId" widgetVar="dlg">
        <iframe frameborder="0" align="left"
               src="#{bean.myURL}" id="someId" scrolling="auto"
               width="600" height="500">
        </iframe>
    </p:dialog>
</h:form>

如您所见,我在 PF Dialog 中使用了一个 iframe,我可以对其进行更新(也可以刷新)。如果您想这样使用它,有一些优点和缺点。对您来说好消息是您可以刷新此对话框。 但是,此解决方案的主要缺点是它不适用于每个外部页面,因为有许多页面由于“X”而无法在框架中显示-Frame-Options 设置(例如 google.com、facebook.com ...),除非您是页面的作者,否则您无法影响。

【讨论】:

  • 我不想有一个 PF 对话框。
  • 请看我的更新评论
猜你喜欢
  • 2015-04-02
  • 1970-01-01
  • 2023-03-14
  • 2015-12-26
  • 1970-01-01
  • 1970-01-01
  • 2017-12-17
  • 1970-01-01
  • 2015-02-21
相关资源
最近更新 更多