Making URL Shortener With Javascript

Making URL Shortener With Javascript - With URL Shortener it will make it easier for us to memorize a URL of our blog and makes it easy for us to share the URL with others.   This will be very useful for those who play affiliates, especially those who use the affiliate landingpage template. So when sharing an affiliate post URL it will look more simple, and will not look like the Blogger posting URL.

Making URL Shortener With Javascript - With URL Shortener it will make it easier for us to memorize a URL of our blog and makes it easy for us to share the URL with others.


This will be very useful for those who play affiliates, especially those who use the affiliate landingpage template. So when sharing an affiliate post URL it will look more simple, and will not look like the Blogger posting URL.

The script is quite simple as follows.


<script>
//<![CDATA[
var key = window.location.href.split("go/")[1].replace("/","")
var urls={
    "kingfbads":"https://www.enterblog.me/2019/11/king-fb-ads.html"
}
if(key){
    if(urls[key]){
        window.location.href=urls[key]
    }else{
        document.write("'"+key+"' not found :(");
    }
}
//]]>
</script>


Jagoanfbads code is a shortener of the post URL https://www.klikblog.me/2019/11/king-fb-ads.html please adjust the shortener and URL of your own post. So from the script above we will get the URL Shortener as follows.

domain_us/go/shortener

Example:

klikblog.me/go/kingfbads
The URL above will automatically redirect to the URL:
https://www.klikblog.me/2019/11/king-fb-ads.html


If you want to add another URL then like this:

<script>
//<![CDATA[
var key = window.location.href.split("go/")[1].replace("/","")
var urls={
    "jagoanfbads":"https://www.klikblog.me/2019/11/king-fb-ads.html",
    "jagoanlp":"https://www.kingblog.me/2019/11/king-landingpage.html"
}
if(key){
    if(urls[key]){
        window.location.href=urls[key]
    }else{
        document.write("'"+key+"' not found :(");
    }
}
//]]>
</script>

Last URL not used, (comma).

But if you only use the script above, it will be an error when the URL is shared with social media, especially Facebook. Because Facebook will automatically add the tracking code to the URL that is shared on Facebook.

For that, I added a script to remove the param in the URL, so the script looks like this :


<script>
//<![CDATA[
var uri = window.location.toString();
    if (uri.indexOf("?") > 0) {
        var clean_uri = uri.substring(0, uri.indexOf("?"));
        window.history.replaceState({}, document.title, clean_uri);
    }
var key = window.location.href.split("go/")[1].replace("/","")
var urls={
    "jagoanfbads":"https://www.klikblog.me/2019/11/king-fb-ads.html"
}
if(key){
    if(urls[key]){
        window.location.href=urls[key]
    }else{
        document.write("'"+key+"' not found :(");
    }
}
//]]>
</script>

Please save the code above the code </head>.