简体   繁体   中英

JavaScript redirect alias to domain

How do I redirect the alias "u144w" to the domain " https://google.com.br " according to the var?? Over time I will add more aliases and domains there, but I can't do a window.location that can take the alias from the URL, and redirect to the domain, what should I do? "Location.pathname" will take the alias, then the "getlink" function should look for that alias and its due domain to which it will redirect, and "location.href" will do the work of redirecting. But I am not able to do this, the most I can use is HTML and JavaScript, and no PHP or MySQL.

<script type="text/javascript">
if(location.pathname == getlink());
{
    function getlink(){
        var links = {
            "u144w": "https://google.com.br",
    };
    window.location.href == getlink();
}
</script>

If I understand you correctly this should work:

<script type="text/javascript">
  var alias_mapping = {
    "u144w": "https://google.com.br"
  }
  if(Object.keys(alias_mapping).includes(location.pathname)){
    window.location.href == alias_mapping[location.pathname];
  }
</script>

To add more aliases just extend alias_mapping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM