Advertisement
muu

Javascript URL shortener

muu
Apr 28th, 2015
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!-- // Pure JS URL shortener, see http://www.lanoiadimuu.it/2014/12/the-cheapest-js-url-shortener-ever/
  2. // Function declaration
  3. function shorturl_redirect() {
  4.     // This JSON array contains all the shorturl/url pairs
  5.     var urls =[ { "shorturl":"?lndm" , "redirect":"http://www.lanoiadimuu.it" },
  6.             { "shorturl":"?example" , "redirect":"http://www.example.com" },
  7.             { "shorturl":"?twitter" , "redirect":"http://twitter.com/lanoiadimuu" },
  8.     // Get the url parameter
  9.     var shorturl = location.search;
  10.     // If there's no url parameter stop script execution
  11.     if(!shorturl || 0 === shorturl.length) { return; }
  12.     // Now the function will look though urls array and if a valid shorturl is found it will redirect the browser to the longer url
  13.     for(i = 0; i < urls.length; i++) {
  14.         if(shorturl == urls[i].shorturl) {
  15.             window.location.replace(urls[i].redirect)
  16.             break;
  17.         }
  18.      }
  19. }
  20. // Function call
  21. shorturl_redirect();
  22. // -->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement