Skip to content

Instantly share code, notes, and snippets.

@jeffreymeng
Last active August 26, 2017 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffreymeng/fdb177ca4654079ade7c848b5466fadf to your computer and use it in GitHub Desktop.
Save jeffreymeng/fdb177ca4654079ade7c848b5466fadf to your computer and use it in GitHub Desktop.
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