JavaScript Universal Slug URL Generator

/** * Usage: * ------ * * <input type="text"> → <input type="text"> * * <script> * var input = document.getElementsByTagName('input'); * slug(input[0], input[1]); * </script> * */ function slug(input, output, connector) { if (typeof connector == "undefined") connector = '-'; input.onkeyup = function() { var v = this.value; // Remove accents ... var from = '¹²³°æǽÀÁÂÃÅǺĂǍÆǼàáâãåǻăǎª@ĈĊĉċ©ÐĐðđÈÉÊËĔĖèéêëĕėƒĜĠĝġĤĦĥħÌÍÎÏĨĬǏĮIJìíîïĩĭǐįijĴĵĹĽĿĺľŀÑñʼnÒÔÕŌŎǑŐƠØǾŒòôõōŏǒőơøǿºœŔŖŕŗŜȘŝșſŢȚŦÞţțŧþÙÚÛŨŬŰŲƯǓǕǗǙǛùúûũŭűųưǔǖǘǚǜŴŵÝŸŶýÿŷЪЬАБЦЧДЕЁЭФГХИЙЯЮКЛМНОПРСШЩТУВЫЗЖъьабцчдеёэфгхийяюклмнопрсшщтувызжÄÖÜßäöüÇĞİŞçğışĀĒĢĪĶĻŅŪāēģīķļņūҐІЇЄґіїєČĎĚŇŘŠŤŮŽčďěňřšťůžĄĆĘŁŃÓŚŹŻąćęłńóśźżΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩΪΫάέήίΰαβγδεζηθικλμνξοπρςστυφχψωϊϋόύώϐϑϒأبتثجحخدذرزسشصضطظعغفقكلمنهويạảầấậẩẫằắặẳẵẹẻẽềếệểễịỉọỏồốộổỗờớợởỡụủừứựửữỳỵỷỹẠẢẦẤẬẨẪẰẮẶẲẴẸẺẼỀẾỆỂỄỊỈỌỎỒỐỘỔỖỜỚỢỞỠỤỦỪỨỰỬỮỲỴỶỸ', to = '1230aeaeAAAAAAAAAEAEaaaaaaaaaatCCcccDjDdjdEEEEEEeeeeeefGGggHHhhIIIIIIIIIJiiiiiiiiijJjLLLlllNnnOOOOOOOOOOOEooooooooooooeRRrrSSsssTTTTHtttthUUUUUUUUUUUUUuuuuuuuuuuuuuWwYYYyyyABCChDEEEFGHIJJaJuKLMNOPRSShShchTUVYZZhabcchdeeefghijjajuklmnoprsshshchtuvyzzhAEOEUEssaeoeueCGIScgisAEGIKLNUaegiklnuGIJiYegijiyeCDENRSTUZcdenrstuzACELNOSZZacelnoszzABGDEZEThIKLMNXOPRSTYPhChPsOIYaeeiYabgdezethiklmnxoprsstyphchpsoiyoyobthYabtthghkhdthrzsshsdtthaaghfkklmnhoyaaaaaaaaaaaaeeeeeeeeiioooooooooooouuuuuuuyyyyAAAAAAAAAAAAEEEEEEEEIIOOOOOOOOOOOOUUUUUUUYYYY'; for (var i = 0, len = from.length; i < len; ++i) { v = v.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i)); } output.value = v.replace(/<.*?>/g, "").replace(/[^a-z0-9-]+/gi, '-').replace(/\-+/g, '-').replace(/^\-|\-$/g, "").toLowerCase().replace(/\-/g, connector); }; }
Used for live preview purpose. The rest should be handled by the backend system → http://codepad.co/s/1b908b

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.