点按钮上的jquery重定向

时间:2017-12-11 04:44:54

标签: javascript jquery wordpress freegeoip

我试图根据访客位置重定向按钮点击,我有两个窗口的模态。以及使用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">&times;</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>

4 个答案:

答案 0 :(得分:3)

美国订阅者

change type="submit" to type="button" //也确保

jQuery('#subscriber-us').on('click', function(){ //check the spelling which is subscribe-us

并在ajax调用之外添加onclick事件 if if else子句只隐藏/显示模态和按钮

答案 1 :(得分:2)

试试此代码

        <!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">&times;</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>

答案 2 :(得分:2)

替换您的代码:

<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>

使用:

<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之外的点击事件。

答案 3 :(得分:1)

要检查这是否是用户的第一次访问,您可以在会话存储中存储值:

window.sessionStorage.setItem("first_visit", "Y");

加载检查会话存储时。如果它不存在则打开模态对话框。

希望它有所帮助。