0

I have a URL http://www.test.com/us/abc/

my CMS only accept html and javascript

Is it possible using javascript to get this current url. and then get "us" parameter and put it in if else statement?

If "us" detected, echo out "US website". If "ca" detected, echo out "Canada website".

Thanks

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = 
window.location.href;
</script>
2
  • You can split the url by '/' then get the third item in splitted array. Commented Sep 14, 2020 at 20:19
  • What CMS are you using? Does it not offer access to the URL and its parts in the template through some sort template language like Liquid? Or are you providing data to the template in which case you can't use the templating language? Commented Sep 14, 2020 at 20:21

1 Answer 1

1
const country = window.location.pathname.split('/')[1]
if (country === 'us') console.log('US website')
if (country === 'ca') console.log('Canada website')

You can find more on location API at MDN.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.