我正在做一个Shopify项目,我创建了一个弹出窗口,当页面加载时会显示,并要求用户在美国和英国网站之间进行选择。
一旦用户点击其中一个选项,弹出窗口要么关闭(英国(,要么重定向到美国网站。然后将其存储在cookie
中,因此当他们再次访问该网站时,会再次重定向/弹出窗口不再显示。
这是代码:
<div class="modal fade" id="storeModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
<div class="subscription-grid">
<div class="grid__item">
<button type="button" class="close" id="subscription-close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<div class="store_popup_heading">
<h2>TEXT</h2>
</div>
<div class="store_popup_content">
<a href="#" class="store-btn" id="uk-store" onClick="checkCookie2();">{{ settings.store_popup_link_text_1 }}</a>
<a href="#" class="store-btn" id="us-store" onClick="checkCookieUS();">{{ settings.store_popup_link_text_2 }}</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
function checkCookieUS() {
Cookies.set('storelocation', 'true', {
expires: 7
});
Cookies.set('storelocationUS', 'true', {
expires: 7
});
}
</script>
<script>
var delayInMilliseconds = 2000
var storelocate = Cookies.get('storelocation');
var storeUS = Cookies.get('storelocationUS');
if (storelocate) {
document.getElementById("storeModal").style.display = "none";
} else {
setTimeout(function() {
document.getElementById("storeModal").style.display = "block";
}, delayInMilliseconds);
}
if (storeUS) {
window.location.href = "https://us.store.com/";
}
</script>
现在这是可行的,但我的问题是,例如,如果有人从一个子页面sohttps://store.com/collections
进入,他们需要重定向到美国商店sohttps://us.store.com/collections
上的同一个子页面。
我试过用来做这件事
if (storeUS) {
window.location.href = "https://us.store.com/{{current_page}}";
}
但是随后重定向到例如https://us.store.com/1
。
有什么办法吗?
您需要获取当前页面的pathname
,并将其附加到https://us.store.com/
。
像这样:
if (storeUS){
window.location = "https://us.store.com" + window.location.pathname;
}