Created
June 30, 2017 09:27
-
-
Save iiic/dcf839ec83ebe4cb7b0e67066d4710b0 to your computer and use it in GitHub Desktop.
Otevírání souborů vložených do stránky v novém okně namísto stejného
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<meta charset="utf-8"> | |
<title>File drop cancel on page</title> | |
<body> | |
<p> | |
Tohle je libovolná stránka, kam se NEmají vkládat soubory pomocí drag & drop. | |
<br> | |
Výchozí chování prohlížečů je takové, že po přenesení souboru do stránky (pomocí drag & drop), | |
tento soubor otevřou v aktuálním okně (čímž přesměrují stránku, co tam byla předtím na nový soubor). | |
Což tedy znamená opuštění stránky :( a to u vlastních stránek nechceme :). | |
Při přetažení více souborů najednou prohlížeče otevřou jen ten první, jen FF otevře všechny (v nových záložkách). | |
</p> | |
<script> | |
(function () { | |
'use strict'; | |
const MAX_FILES_TO_OPEN = 5; | |
function manageFiles(event) { | |
event.stopPropagation(); | |
event.preventDefault(); | |
var files = event.target.files || event.dataTransfer.files; | |
if (files.length > 0 && files.length < MAX_FILES_TO_OPEN) { | |
for (var i = 0; i < files.length; i++) { | |
if (window.navigator.msSaveOrOpenBlob) { // IE | |
window.navigator.msSaveOrOpenBlob(files[i], files[i].name); | |
} else { // Other browsers | |
window.open(URL.createObjectURL(files[i])); | |
} | |
} | |
} | |
} | |
document.addEventListener('dragover', manageFiles, false); | |
document.addEventListener('dragleave', manageFiles, false); | |
document.addEventListener('drop', manageFiles, false); | |
}()); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment