Created
May 4, 2009 17:36
-
-
Save ericabouaf/106566 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Permalink methods with TinyUrl via JSONP</title> | |
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/utilities/utilities.js"></script> | |
<script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/json/json-min.js"></script> | |
</head> | |
<body> | |
<script> | |
var Permalink = { | |
load: function() { | |
var s = window.location.search.substr(1); | |
if( s.substr(0,10) != "permalink=" ) return null; | |
return YAHOO.lang.JSON.parse(window.decodeURIComponent(s.substr(10))); | |
}, | |
get: function(config) { | |
var l = window.location, pl = YAHOO.lang.JSON.stringify(config); | |
return l.protocol+"//"+l.host+l.pathname+"?permalink="+pl; | |
}, | |
getTiny: function(config, success) { | |
var longURL = this.get(config), | |
ud = 'pl'+(Math.random()*100).toString().replace(/\./g,''), | |
API = 'http://json-tinyurl.appspot.com/?url=', | |
url = API + encodeURIComponent(longURL) + '&callback=' + ud; | |
window[ud]= function(o){ success&&success(o.tinyurl); }; | |
document.getElementsByTagName('body')[0].appendChild((function(){ | |
var s = document.createElement('script'); | |
s.type = 'text/javascript'; | |
s.src = url; | |
return s; | |
})()); | |
} | |
}; | |
var loadConfig = Permalink.load(); | |
console.log( loadConfig ); | |
var config = loadConfig || {"hello":"World","n":9} | |
var pl = Permalink.get(config); | |
console.log(pl); | |
Permalink.getTiny( config, function(tinyurl){ | |
var a = document.getElementById('link'); | |
a.href=tinyurl; | |
a.innerHTML = tinyurl; | |
}); | |
</script> | |
<a href="" id='link'/> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment