JavaScript window.open 仅当窗口不存在时才打开

编程入门 行业动态 更新时间:2024-10-10 23:19:37
本文介绍了JavaScript window.open 仅当窗口不存在时才打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个应用程序,在单击链接时会打开一个新窗口.这会生成一个包含 Java 小程序的页面.我遇到的问题是单击相同的链接会重新加载页面,从而重置 Java 应用程序.有什么办法可以捕获这个吗?两种可以接受的解决方案是:

I have an application that opens a new window on clicking a link. This spawns a page that holds a Java applet. The problem I am having is that clicking the same link reloads the page, which resets the Java application. Is there any way to trap this? Two solutions that would be acceptable are:

  • 允许从点击处理程序打开多个窗口
  • 如果窗口已经打开,则忽略后续请求
  • 为成为 Javascript 新手而道歉 - 这并不是我的主要工作.

    Apologies for being a Javascript newbie - it's not really my main thing.

    附加到处理程序的代码是

    The code attached to the handler is

    function launchApplication(l_url, l_windowName) { var l_width = screen.availWidth; var l_height = screen.availHeight; var l_params = 'status=1' + ',resizable=1' + ',scrollbars=1' + ',width=' + l_width + ',height=' + l_height + ',left=0' + ',top=0'; winRef = window.open(l_url, l_windowName, l_params); winRef.moveTo(0,0); winRef.resizeTo(l_width, l_height); }

    感谢您的回复 - 我稍微修改了建议,以便可以通过该函数打开多个 URL.

    Thanks for the replies - I modified the suggestions slightly so that I could have more than one URL opened via the function.

    此代码的另一个版本位于 检查在另一个窗口中打开的 URL

    There is another version of this code at Check for a URL open on another window

    var g_urlarray = []; Array.prototype.has = function(value) { var i; for (var i in this) { if (i === value) { return true; } } return false; }; function launchApplication(l_url, l_windowName) { var l_width = screen.availWidth; var l_height = screen.availHeight; var winRef; var l_params = 'status=1' + ',resizable=1' + ',scrollbars=1' + ',width=' + l_width + ',height=' + l_height + ',left=0' + ',top=0'; if (g_urlarray.has(l_url)) { winRef = g_urlarray[l_url]; } alert(winRef); if (winRef == null || winRef.closed) { winRef = window.open(l_url, l_windowName, l_params); winRef.moveTo(0,0); winRef.resizeTo(l_width, l_height); g_urlarray[l_url] = winRef; } }

    推荐答案

    我会这样做 - 基本上将所有引用的打开窗口存储在函数本身上.当函数触发时,检查窗口是否不存在或已关闭 - 如果是这样,启动弹出窗口.否则,请关注该请求的现有弹出窗口.

    I'd do it like this - basically store all the referenced opened windows on the function itself. When the function fires, check if the window doesn't exist or has been close - of so, launch the popup. Otherwise, focus on the existing popup window for that request.

    function launchApplication(l_url, l_windowName) { if ( typeof launchApplication.winRefs == 'undefined' ) { launchApplication.winRefs = {}; } if ( typeof launchApplication.winRefs[l_windowName] == 'undefined' || launchApplication.winRefs[l_windowName].closed ) { var l_width = screen.availWidth; var l_height = screen.availHeight; var l_params = 'status=1' + ',resizable=1' + ',scrollbars=1' + ',width=' + l_width + ',height=' + l_height + ',left=0' + ',top=0'; launchApplication.winRefs[l_windowName] = window.open(l_url, l_windowName, l_params); launchApplication.winRefs[l_windowName].moveTo(0,0); launchApplication.winRefs[l_windowName].resizeTo(l_width, l_height); } else { launchApplication.winRefs[l_windowName].focus() } }

    更多推荐

    JavaScript window.open 仅当窗口不存在时才打开

    本文发布于:2023-07-21 03:38:01,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1173710.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:不存在   时才   窗口   JavaScript   window

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!