How to prevent a blank tab from appearing in Edge

2019-03-01 00:05发布

问题:

I have a html page with several links to files with various file types, such as pdf, csv, and zip. Depending on the available browser plugins, some of these files can be opened inline by the browser, whereas others will be downloaded.

I don't want such links to open in the current tab, so each one has the attribute target="blank".

This works fine in most browsers:

  • When the user clicks on a link to a file that can be displayed inline, the file is shown in a new tab.
  • Otherwise, a new tab is opened and immediately closed as soon as the file starts to download. The user stays in the current window.

In Microsoft Edge, however, the second case does not work: the new tab remains open. This is annoying, because the user is now looking at a useless empty tab.

Is there any way to prevent this from happening?

回答1:

I don't think there is anything you can prevent Edge's this behaviour. What you can do is to change the HTML tag.

Use download attribute in <a> element without target attribute. This way, the browser will prompt save dialog instead of opening a new tab.

<a href="myfile" download>Download</a>

http://www.w3schools.com/tags/att_a_download.asp

In this case, the browser will not display the file inline.

If you still want your clients be able to see the files inline you can detect the client's browser; if it is Edge then use the download attribute, if not use target attribute. In addition, you can use something like navigator.mimetypes to detect which file types can be displayed inline (see https://developer.mozilla.org/en-US/docs/Web/API/NavigatorPlugins/mimeTypes).

Here is the detect function which I took from another post (How can I detect Internet Explorer (IE) and Microsoft Edge using JavaScript?)

function isEDGE(){
 return /Edge\/\d./i.test(navigator.userAgent)
}

Leave your <a> tags with no target and download attributes. Use detect function and decide on the right attribute.

Like this:

$(document).ready(function(){
    if(isEDGE()) {
        $('a').attr('download','download');
    } else {
        $('a').attr('target','_blank');
    }
})

Note: I am not sure about Edge detecting function.



回答2:

Method 1: I suggest you clear the Clear browsing data option of Microsoft Edge and check if you face the issue. To do so perform the steps below. Click on the More actions icon next to the feedback icon present on top right corner of the Edge. Select Settings and click on Choose what to clear. Check the boxes Browsing history, Cookies and saved website data and Cached data and files and click on Clear.

Method 2: If you are using any Proxy connection, then try disabling the proxy connection and check. Follow the steps to disable proxy: Click the Settings icon at the top right corner in internet explorer. Click the Tools button, and then click Internet Options. Click the Connections tab, and then click LAN settings. Uncheck the box next to “proxy server for your LAN”. Click on OK to save the setting and close window. Now check the issue by opening Edge.