Skip to content

Instantly share code, notes, and snippets.

@pottava
Created November 12, 2012 06:09
Show Gist options
  • Save pottava/4057780 to your computer and use it in GitHub Desktop.
Save pottava/4057780 to your computer and use it in GitHub Desktop.
サーバアクセスなしにローカルファイルを背景画像に適用するjs
// change BgImage by the input file's attribute without any server access.
document.querySelector('input[type="file"]').addEventListener('change', function (e) {
var reader = new FileReader();
reader.onload = function(e) {
$('#backgroundId').css({backgroundImage:'url("'+e.target.result+'")'});
};
reader.readAsDataURL(e.target.files[0]);
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment