<html>
<head>
<meta charset=utf-8 />
<title>dataurl for image</title>
<style>
html, body { font-family: sans-serif; height: 100%; }
* { margin: 0; box-sizing: border-box; box-sizing: border-box; box-sizing: border-box; }
body { margin-top: 28px; }
iframe { width: 100%; height: 100%; }
pre { width: 100%; }
#source { top: 0; width: 100%; position: fixed; display: block; padding: 5px; color: #fff; background: #ccc; text-decoration: none; text-align: center; }
textarea { border: 0; height: 100%; padding: 10px; width: 100%; font-family: courier; font-size: 16px; }
#start { font-size: 36px; text-align: center; margin: 100px; }
[hidden] { display: none; }
</style>
</head>
<body>
<a id=source hidden href="#">Source</a>
<p id=start>Drag and drop files in this window to read it's contents, and get data-urls for images or video</p>
</body>
</html>
var holder = document.documentElement,
el = document.querySelector('#start'),
link = document.querySelector('a');
holder.ondragover = function () { return false; };
holder.ondragend = function () { return false; };
holder.ondrop = function (e) {
e.preventDefault();
if (el) document.body.removeChild(el);
var file = e.dataTransfer.files[0],
reader = new FileReader(),
type = 'data';
if (file.type.indexOf('image/') === 0) {
el = new Image();
} else if (file.type.indexOf('video/') === 0) {
el = document.createElement('video');
} else if (file.type.indexOf('audio/') === 0) {
el = document.createElement('audio');
} else if (file.type.indexOf('text/html') === 0) {
el = document.createElement('iframe');
type = 'text';
} else {
el = document.createElement('pre');
type = 'text';
}
reader.onload = function (event) {
if (file.type.indexOf('image/') === 0 || file.type.indexOf('video/') === 0) {
el.src = event.target.result;
} else if (file.type.indexOf('text/html') === 0) {
console.log(el, el.contentDocument)
el.contentDocument.documentElement.innerHTML = event.target.result;
} else {
el.innerHTML = event.target.result;
}
var href = '';
if (type == 'text') {
href = 'data:' + file.type + ',' + event.target.result;
} else {
href = event.target.result;
}
link.href = href;
link.hidden = false;
};
console.log(file, type);
document.body.appendChild(el);
reader[type == 'data' ? 'readAsDataURL' : 'readAsText'](file);
return false;
};
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |