1

リンクを 1 回クリックするだけで 2 つのページを開こうとしています。

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

および Javascript 関数:

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

上記のコードは FireFox と Safari では完全に動作しますが、Google Chrome では新しいウィンドウを開くことができません。どうしてこれなの?助けてくれる人に感謝します。

4

2 に答える 2

3

<a>ここで説明されているように、要素には HTML5 の download 属性があり、デフォルト値は "" (空の文字列) です。

これは、onclick ハンドラー (これは onevent 属性の要素です) で download === this.download を意味するため、この要素の download 属性は window の download プロパティよりも優れています。

ああ、なんて悪夢だ。関数に download() という名前を付けないでください。関数名を download1() に変更し、onclick も download1() に変更します

于 2013-03-03T21:32:35.010 に答える
0

HTML5 ダウンロード属性を使用できます。この属性は、作成した仮想リンクがダウンロード専用であることをブラウザに伝えます。リンクs href to file with name specified as download attributeの値からファイルをダウンロードします。この機能は Chrome で動作します。サンプルコード:

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;
于 2014-05-30T07:19:02.530 に答える