javascript - window.open not working with Google Chrome? - Stack Overflow

I'm trying to open 2 pages with one click of a link, and this is what I have so far:<a onclick=

I'm trying to open 2 pages with one click of a link, and this is what I have so far:

<a onclick="download()" href="?event=thanks&dl=<?php echo $_GET['dl']; ?>"><?php echo $linkname ?></a>

and the Javascript function:

function download() {
    newwindow=window.open('','download','height=200,width=150');
    if (window.focus) {newwindow.focus()}
    return false;
}

The code above works perfectly with FireFox and Safari, but it fails to open a new window with Google Chrome. Why is this? My thanks to anyone who can help.

I'm trying to open 2 pages with one click of a link, and this is what I have so far:

<a onclick="download()" href="?event=thanks&dl=<?php echo $_GET['dl']; ?>"><?php echo $linkname ?></a>

and the Javascript function:

function download() {
    newwindow=window.open('http://www.google.','download','height=200,width=150');
    if (window.focus) {newwindow.focus()}
    return false;
}

The code above works perfectly with FireFox and Safari, but it fails to open a new window with Google Chrome. Why is this? My thanks to anyone who can help.

Share Improve this question edited Mar 3, 2013 at 21:27 DaveRandom 88.7k11 gold badges158 silver badges173 bronze badges asked Mar 3, 2013 at 21:19 Mrdoctor KovacicMrdoctor Kovacic 972 silver badges6 bronze badges 3
  • see 1 and 2 – user1646111 Commented Mar 3, 2013 at 21:21
  • Check the Console in Chrome’s Inspector. That’ll notify you of any errors. – Martin Bean Commented Mar 3, 2013 at 21:21
  • It may not like the implied global variable newwindow in the above code, have you explicitly declared it in a higher scope anywhere else in the script? If not, and you don't need to keep the reference to the created window, try simply prefixing the first line of the JS function with var. – DaveRandom Commented Mar 3, 2013 at 21:30
Add a ment  | 

2 Answers 2

Reset to default 3

<a> elements have a download attribute in HTML5 as explained here, with a default value of "" (an empty string).

This means that download === this.download in the onclick handler (this is the element in onevent attributes), and therefore the download attribute of the element is superior to the download property of window.

Oh, what a nightmare. Your function should not named download(). Change your function name to download1() and change your onclick to download1() too

You can use HTML5 download attribute.This attribute will tell browser that virtual link we created is aimed for download only. It will download file from links href to file with name specified as download attributes value. This feature works with Chrome. Sample Code:

window.downloadFile = function(sUrl) {

    //If in Chrome or Safari - download via virtual link click
    if (window.downloadFile.isChrome || window.downloadFile.isSafari) {
        //Creating new link node.
        var link = document.createElement('a');
        link.href = sUrl;

        if (link.download !== undefined){
            //Set HTML5 download attribute. This will prevent file from opening if supported.
            var fileName = sUrl.substring(sUrl.lastIndexOf('/') + 1, sUrl.length);
            link.download = fileName;
        }

        //Dispatching click event.
        if (document.createEvent) {
            var e = document.createEvent('MouseEvents');
            e.initEvent('click' ,true ,true);
            link.dispatchEvent(e);
            return true;
        }
    }

    // Force file download (whether supported by server).
    var query = '?download';

    window.open(sUrl + query);
}

window.downloadFile.isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') &gt; -1;
window.downloadFile.isSafari = navigator.userAgent.toLowerCase().indexOf('safari') &gt; -1;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745318435a4622329.html

相关推荐

  • javascript - window.open not working with Google Chrome? - Stack Overflow

    I'm trying to open 2 pages with one click of a link, and this is what I have so far:<a onclick=

    7小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信