Making URL Shortener With
Javascript - With URL Shortener it will make it easier for us to memorize a URL
of our blog and make it easy for us to share the URL with others.
This will be very useful for
those who play affiliates, especially those who use affiliate landingpage
templates . So when sharing an affiliate's post URL it will appear more simple,
and it won't look like the Blogger post URL.
The script is quite simple
as follows.
<script>
//<![CDATA[
var key = window.location.href.split("go/")[1].replace("/","")
var urls={
"jagoanfbads":"https://www.enterblog.me/2019/11/jagoan-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.enterblog.me/2019/11/jagoan-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_kita/go/shortener
Example:
enterblog.me/go/jagoanfbads
The URL above will automatically redirect to the URL:
https://www.enterblog.me/2019/11/jagoan-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.enterblog.me/2019/11/jagoan-fb-ads.html",
"jagoanlp":"https://www.enterblog.me/2019/11/jagoan-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.enterblog.me/2019/11/jagoan-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> .