打开自定义协议后关闭浏览器窗口 [英] Close browser window after opening Custom Protocol

查看:19
本文介绍了打开自定义协议后关闭浏览器窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 ASP.Net 应用程序页面来处理打开 FileSite 链接.有一个自定义协议可以正确处理链接,即它打开文件,但是在文件启动时它给我留下一个空的浏览器页面.

I have created an ASP.Net application page to handle opening FileSite links. There is a custom protocol which is handling the links correctly, i.e it opens the files, however it leaves me with an empty browser page as the file is launched.

我正在处理 3 个场景

I have 3 scenarios I am working with

  1. 直接链接到处理页面将启动文件并关闭浏览器
  2. 来自 Intranet 上其他页面的链接将启动处理页面,打开文件并返回原始页面
  3. 来自内联网对话框的链接打开处理页面,启动文件,然后关闭处理页面

我的代码如下(Codebehind 是设置 FileUrl 并选择要调用的两个函数)

The code I have is the following (Codebehind is setting the FileUrl and choosing which function to call of the two)

<script type="text/javascript" language="javascript">

    // Files opened directly from link
    function OpenFileSiteLink() {
        window.location.href = '<%= FileUrl %>';
    }

    // Files opened from within Intranet
    function OpenFileSiteLinkReferrer(referrer, dialogOpened) {

        window.open('<%= FileUrl %>');

        if (dialogOpened) {
            window.open('close.html', '_self');
        } else {
            window.location.href = referrer;
        }
    }

</script>

close.html 文件中的代码只有以下内容

The code in the close.html file has only the following

 <script type="text/javascript"> window.close();</script>

这取自 如何在不收到您要关闭此窗口"的情况下关闭浏览器窗口?提示?

对于如何在没有附加对话框的情况下打开协议以启动应用程序的任何建议,我们将不胜感激

Any suggestions how I can open the protocol to launch the application without the additional dialog would be appreciated

推荐答案

最简单、最可靠的方法实现起来却是最烦人的.不幸的是,IE 9/10 和 Firefox 已经插入了完成此操作的常规方法,因此您可能别无选择.

The least hacky and most reliable method to do this is the most annoying to implement. Unfortunately, IE 9/10 and Firefox have plugged up the regular methods of accomplishing this, so you may have no other choice.

策略是从父窗口弹出,而不是直接打开它.您需要创建一个函数以在弹出窗口中加载适当的 url,然后将其应用于链接到处理程序 url 的每个链接的 onclick.然后,将此脚本包含在您网站的每个页面上.我还假设每个文件 url 都是唯一的,但具有某种通用的基本 url.如果不是这种情况,您还需要设置标识类或属性.进行替换的函数类似于:

The strategy is to come at the popup from the parent window, rather than opening it directly. You will need to create a function to load the appropriate url in a popup, and then apply it to the onclick of every link on your linking to the url of the handler. Then, include this script on every page on your site. I am also assuming that each file url is unique, but has a common base url of some kind. If this is not the case, you will also need to set an identifying class or attribute. The function to do the replacement would be something along the lines of:

var replaceHandlerLinks = function () {
    var fileLinks = document.querySelectorAll("[href*='/beginning/to/file/path']");
    for (var i = 0; i < fileLinks.length; i++) {
        fileLinks[i].onclick = function () {
            var fileOpener = window.open(fileLinks[i].href);
            //depending on how long your file takes to load, you may need to wait here
            fileOpener.close();
        }
    }
}

明显的缺点是指向这些文件的任何链接都需要来自您控制的页面.另外,请注意我使用 document.querySelectorAll 来定位链接 href 元素.这意味着按钮等不适用于此特定功能.对 JQuery 使用更兼容浏览器/更健壮的查询,或者在所有必需的按钮、链接等上设置一个类,将使其成为一种更完整的方法.

The obvious downside of this being that any link to these files would need to originate from pages you control. Additionally, note that I am using document.querySelectorAll to target the links href element. This means that buttons etc will not work with this particular function. Using a more browser compatible/robust query with JQuery or setting a class on all required buttons,links,etc would make this a more complete approach.

另一种类似的方法,如果您需要从您无法控制的页面进行链接,则始终使用参数从任何地方链接到同一页面,例如/openfile.aspx?file=actualFilePath",然后从该页面打开一个弹出窗口来加载文件,观察新窗口并在文件完成后关闭它.然后,尝试根据场景重定向当前窗口:1) 转到主页/登陆/等 2) 转到引荐来源 3) 如果可能,转到内网登陆/引荐来源网址.这不像从内部关闭弹出窗口那么优雅,但它解决了丑陋的空白窗口.

Another similar approach, if you need to link from pages you do not control, would be to always link to the same page from everywhere with a param, eg "/openfile.aspx?file=actualFilePath", then from that page open a popup to load the file, watch the new window and close it when the file is done. Then, attempt to redirect the current window based on scenario: 1) go to homepage/landing/etc 2) go to referrer 3) go to intranet landing/ referrer if possible. This isn't as elegant as just closing a popup from within itself, but it addresses the ugly blank window.

这两种方法都不是完美的,但除了将用户限制为 ie8- 和 chrome 之外,您可能别无选择.

Neither approach is perfect, but outside of restricting users to ie8- and chrome, you may not have any other choice.

这篇关于打开自定义协议后关闭浏览器窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆