Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Works on any html file with images(word file saves as html, raw html file, etc.)

Usage: Works on any html file. When you have any html file with images(using src tag), and a user clicks on an image element, that image(the src) will automatically open in a new tab or window(depending on user/browser prefrence)

All you have to do is include the quick-use.html file's raw code(the script element and its contents) and add it to the end of your html(right before the closing body tab, or with all your other scripts. AFTER the images).

<script>
var elements = document.querySelectorAll("img");Array.prototype.forEach.call(elements, function(el, i) {
el.addEventListener("click", function() {window.open(el.getAttribute("src"), '_blank');});});
</script>
var elements = document.querySelectorAll("img");
Array.prototype.forEach.call(elements, function(el, i) {
el.addEventListener("click", function() {
window.open(el.getAttribute("src"), '_blank');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment