Suppose I have this URL:
www.example.com/product-p/xxx.htm
The following javascript code picks out the phrase product-p
from the URL:
urlPath = window.location.pathname;
urlPathArray = urlPath.split('/');
urlPath1 = urlPathArray[urlPathArray.length - 2];
I can then use document.write to display the phrase product-p
:
document.write(''+urlPath1+'')
My question is...
How do I create an IF statement such that if urlPath1 = 'product-p' then document.write (something), else document.write (blank)?
I have tried to do this but my syntax is probably wrong (I'm not too good at JS).
I initially thought the code would be this:
<script type="text/javascript">
urlPath=window.location.pathname;
urlPathArray = urlPath.split('/');
urlPath1 = urlPathArray[urlPathArray.length - 2];
if (urlPath1 = "product-p"){
document.write('test');
}
else {
document.write('');
}
</script>