Skip to content

Instantly share code, notes, and snippets.

@atk
Forked from 140bytes/LICENSE.txt
Created October 17, 2011 10:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save atk/1292379 to your computer and use it in GitHub Desktop.
Save atk/1292379 to your computer and use it in GitHub Desktop.
QR-Codify - add QR-Code to link

QR-Codify

Small JS snippet to add an QR code to a given link.

Usage

func([link node]) // -> Adds a 99x99px QR Code with the href to the node

function(
l, // link node
i // placeholder for image
){
// add newly created image i (currently without source) to link
l.appendChild(
i=new Image()
);
// set image source to use the google chart api to create the QR-Code
i.src='//chart.apis.google.com/chart?cht=qr&chs=99x99&chld=M|0&chl='+l.href;
}
function(l,i){l.appendChild(i=new Image());i.src='//chart.apis.google.com/chart?cht=qr&chs=99x99&chld=M|0&chl='+l.href;}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Alex Kloss <alexthkloss@web.de>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "QRcodify",
"description": "Adds QR-Code to link",
"keywords": [
"link",
"QR-Code",
"smartphone"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected result: the following link contains a QR-Code pointing to the 140byt.es Homepage:</div>
<div>Result:<br/><a id="ret" href="http://140byt.es">140byt.es </a></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var myFunction = function(l,i){l.appendChild(i=new Image());i.src='//chart.apis.google.com/chart?cht=qr&chs=99x99&chld=M|0&chl='+l.href;}
myFunction(document.getElementById( "ret" ));
</script>
@tsaniel
Copy link

tsaniel commented Oct 17, 2011

What about
function(l){l.appendChild(new Image).src='//chart.apis.google.com/chart?cht=qr&chs=99x99&chld=M|0&chl='+l.href}
?

@atk
Copy link
Author

atk commented Oct 17, 2011

Nice one. Will have to test it in multiple browsers before I make the change.

@tsaniel
Copy link

tsaniel commented Oct 17, 2011

And maybe we could also use some URL shortening service to make the URL shorter?

@atk
Copy link
Author

atk commented Oct 17, 2011

We could, but that'd add additional request overhead to the browser.

@jed
Copy link

jed commented Oct 17, 2011

wouldn't it make more sense to just return the image? not sure the append step belongs in the API... would rather see an async function called back on onload.

function(a,b,c){c=new Image;c.src="//chart.apis.google.com/chart?cht=qr&chs=99x99&chld=M|0&chl="+a;c.onload=function(){b(c)}}

EDIT: okay i take it back, i see what you're doing here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment