teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

ホスト部のハードコードを除去

2019/10/09 12:28

投稿

thyda.eiqau
thyda.eiqau

スコア2982

answer CHANGED
@@ -2,4 +2,17 @@
2
2
  ```js
3
3
  window.location.href = `https://sample.com/${Math.random() < 0.5? 'B' : 'C'}${window.location.search}`;
4
4
  ```
5
- ではダメでしょうか
5
+ ではダメでしょうか
6
+
7
+ 追記 Oct 9, 2019 9:30pm
8
+ ホストは変わらないのですね。
9
+ ```js
10
+ window.location.href = `${window.location.origin}/${Math.random() < 0.5? 'B' : 'C'}${window.location.search}`;
11
+
12
+ // 上のだと1行が長くなって見づらいので、分割して書くならこれでもいいかと
13
+ window.location.href = [
14
+ window.location.origin,
15
+ Math.random() < 0.5? '/B' : '/C',
16
+ window.location.search
17
+ ].join('');
18
+ ```