次の URL があるとします。
www.example.com/product-p/xxx.htm
次の JavaScript コードはproduct-p
、URL からフレーズを選択します。
urlPath = window.location.pathname;
urlPathArray = urlPath.split('/');
urlPath1 = urlPathArray[urlPathArray.length - 2];
次に、document.write を使用して次のフレーズを表示できますproduct-p
。
document.write(''+urlPath1+'')
私の質問は...
if urlPath1 = 'product-p' then document.write (something), else document.write (blank) のような IF ステートメントを作成するにはどうすればよいですか?
私はこれをやろうとしましたが、私の構文はおそらく間違っています (私は JS があまり得意ではありません)。
私は当初、コードは次のようになると考えていました。
<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>