Small JS snippet to add an QR code to a given link.
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 <[email protected]> | |
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> |
Nice one. Will have to test it in multiple browsers before I make the change.
And maybe we could also use some URL shortening service to make the URL shorter?
We could, but that'd add additional request overhead to the browser.
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!
What about
function(l){l.appendChild(new Image).src='//chart.apis.google.com/chart?cht=qr&chs=99x99&chld=M|0&chl='+l.href}
?