我正在尝试根据访问者位置重定向按钮单击按钮,并且我的模式有两个窗口。以及使用freegeoip.net:我将其与WordPress:脚本正确地挂在WordPress和运行中,但重定向不起作用。
我同样希望这种模态每次访问一次仅出现一次,并且通过网站的所有导航在每个页面上都可以重新加载。预先感谢。
下面是jQuery:
jQuery(document).ready(function(){
jQuery("#myModal").modal('show');
jQuery.ajax({
url: 'https://freegeoip.net/json/',
type: 'POST',
dataType: 'jsonp',
success: function(location) {
// If the visitor is browsing from Canada.
if (location.country_code === 'CA') {
jQuery('#subscriber-ca').on('click', function(){
window.location.replace('http://www.google.com');
});
jQuery('#subscriber-us').on('click', function(){
// Redirect visitor to the USA.
window.location.replace('http://www.facebook.com');
});
} else if(location.country_code === 'US') {
jQuery('#subscriber-ca').on('click', function(){
window.location.replace('http://www.google.com');
});
jQuery('#subscriber-us').on('click', function(){
// Redirect visitor to the USA.
window.location.replace('http://www.facebook.com');
});
}
}
});
});
这是模态:
<div id="myModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Subscribe our Newsletter</h4>
</div>
<div class="modal-body">
<p>Subscribe to our mailing list to get the latest updates straight in your inbox.</p>
<form>
<div class="form-group">
<input type="text" class="form-control" placeholder="Name">
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Email Address">
</div>
<button id="subscribe-us" type="submit" class="btn btn-primary pull-left">US Subscriber</button>
<button id="subscribe-ca" type="submit" class="btn btn-primary pull-right">Canada Subscriber</button>
</form>
</div>
</div>
</div>
</div>
尝试此代码
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div id="myModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Subscribe our Newsletter</h4>
</div>
<div class="modal-body">
<p>Subscribe to our mailing list to get the latest updates straight in your inbox.</p>
<form>
<div class="form-group">
<input type="text" class="form-control" placeholder="Name">
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Email Address">
</div>
<button id="ussubscribe" type="button" class="btn btn-primary pull-left">US Subscriber</button>
<button id="casubscribe" type="button" class="btn btn-primary pull-right">Canada Subscriber</button>
</form>
</div>
</div>
</div>
</div>
<script>
var locationstatus = false;
var userlocation = '';
jQuery(document).ready(function(){
jQuery.ajax({
url: 'https://freegeoip.net/json/',
type: 'POST',
dataType: 'jsonp',
success: function(locationresponse) {
userlocation = locationresponse.country_code;
locationstatus = true;
console.log(userlocation);
}
});
});
$('#ussubscribe').on('click', function(){
alert();
console.log(userlocation);
if (locationstatus) {
if(locationstatus=='CA'){
window.location.replace('http://www.google.com');
}
}
});
$('#casubscribe').on('click', function(){
console.log(userlocation);
if (locationstatus) {
if(locationstatus=='US'){
window.location.replace('http://www.facebook.com');
}
}
});
</script>
</body>
</html>
us subscriber
change type="submit" to type="button"
//还确保
jQuery('#subscriber-us').on('click', function(){ //check the spelling which is subscribe-us
并在Ajax呼叫之外添加onclick事件内部如果其他条款仅隐藏/显示模式和按钮
替换您的代码:
<button id="subscribe-us" type="submit" class="btn btn-primary pull left">US Subscriber</button>
<button id="subscribe-ca" type="submit" class="btn btn-primary pull-right">Canada Subscriber</button>
with:
<button id="subscriber-us" type="button" class="btn btn-primary pull left">US Subscriber</button>
<button id="subscriber-ca" type="button" class="btn btn-primary pull-right">Canada Subscriber</button>
并绑定单击ajax
外的单击事件,如果buttons
未在ajax request
上动态创建。
检查这是否是用户的第一次访问,您可以将值存储在会话存储中:
window.sessionStorage.setItem("first_visit", "Y");
加载会话存储检查时。如果不存在,请打开模态对话框。
希望它有帮助。