<html>
<head>
<meta name="description" content="Prevent uploading large files">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<style>* {font-family: sans-serif}</style>
</head>
<body>
<form action="">
<p data-maxsize-message>Files larger than {MAX_SIZE} aren't allowed</p>
<input type="file" name="uploadables" multiple><br>
<div data-error-wrapper></div>
<button type="submit" name="submit" value="submit">Submit</button>
</form>
</body>
</html>
const MAX_SIZE = 0.2e6; // bytes => ~0.2MB
const MAX_SIZE_STR = '0.2MB';
const form = document.querySelector('form');
const fileInputs = form.querySelectorAll('[type=file]', form);
const errorWrapper = form.querySelector('[data-error-wrapper]');
const submitButton = form.querySelector('[type=submit]');
const maxSizeMessage = form.querySelector('[data-maxsize-message]');
// Display max allowable file size
maxSizeMessage.innerText = maxSizeMessage.innerText
.replace('{MAX_SIZE}', MAX_SIZE_STR);
// listen to all file inputs for new files
[].slice.call(fileInputs)
.forEach(inp => inp.addEventListener('change', handleFile));
function handleFile(e) {
let files = [].slice.call(this.files);
// get names and filesizes for all large files
let largeFiles = files
.filter(({size}) => size > MAX_SIZE)
.map(({name, size}) => ({name, size}));
// if there are large files, show a message and prevent upload
if (largeFiles.length) {
let errors = largeFiles.map(({name, size}) => {
return `${name} is larger than ${MAX_SIZE_STR}`;
});
errorWrapper.innerText = errors.join('\n');
submitButton.disabled = true;
// prevent upload by removing input name (restored with valid input)
this.dataset.name = this.name;
this.name = '';
} else {
errorWrapper.innerText = '';
submitButton.removeAttribute('disabled');
// restore input names
this.name = this.dataset.name;
}
}
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. |