1

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.com','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.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • see [1](http://stackoverflow.com/questions/2572333/google-chrome-window-open-workaround) and [2](http://stackoverflow.com/questions/4994063/setting-the-page-title-of-chrome-window-open) –  Mar 03 '13 at 21:21
  • Check the Console in Chrome’s Inspector. That’ll notify you of any errors. – Martin Bean Mar 03 '13 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 Mar 03 '13 at 21:30

2 Answers2

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

MIIB
  • 1,849
  • 10
  • 21
  • Sorry, why would this make any difference whatsoever based on the above code? – DaveRandom Mar 03 '13 at 21:33
  • I think chrome has default function download for itself and because of this , the above code is not working as the user tries to call download function – MIIB Mar 03 '13 at 21:34
  • Chrome does not have a global member called `download`, nothing does unless you create one. – DaveRandom Mar 03 '13 at 21:36
  • in html5 `a` element has an attribute named download. I edited my post look at it now – MIIB Mar 03 '13 at 21:37
0

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;
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Vishal
  • 171
  • 7