如何一个接一个地执行jquery函数

时间:2015-12-11 05:43:49

标签: javascript php jquery html

在下面,当我点击按钮调用jquery点击事件时,它将首先显示警报,直到它正常工作,在此我调用了两个函数(即reload()和popup())当我点击按钮它会发出警报,之后它会一次执行popup()函数和reload()函数。 我的查询是我想要执行第一次重装功能(它只重装一次)然后执行弹出功能意味着当我点击按钮时首先重新加载页面一次,然后显示我的弹出窗口。

<script>
  $(document).ready(function()
              { 

                  $('.button').click(function()
                  {

                      var href = $(this).val();
                      alert(href);
                      $.session.set("linkurl",href);
                      alert($.session.get('linkurl'));
                      //window.location.reload();

                        function reload()
                        {
                            if(document.URL.indexOf("#")==-1)
                            {
                            // Set the URL to whatever it was plus "#".
                            url = document.URL+"#";
                            location = "#";

                            //Reload the page
                            location.reload(true);
                            return true;
                            }

                        }
                        function popup()
                        {
                            document.getElementById('light').style.display='block';
                            document.getElementById('fade').style.display='block';
                        }
                        $.when( reload() ).done(function() {
                            popup();
                        });

                  }); 
              });
</script>



<button  class="button"  value="<?php echo $value['3']; ?>" style="background-color:#ff8c21;">Buy Now</button>

3 个答案:

答案 0 :(得分:0)

简单的答案 - 它不起作用。您无法重新加载页面并在其后执行任何JavaScript。卸载页面后,JavaScript将停止执行。

您需要将任何类型的标记传递到目标页面,以告诉它显示弹出窗口。例如,您可以使用GET参数。

关于这个主题有一篇很好的Stackoverflow文章:
Global Variable usage on page reload

答案 1 :(得分:0)

 $(document).ready(function() {

  $('.button').click(function() {

    var href = $(this).val();
    alert(href);
    $.session.set("linkurl", href);
    alert($.session.get('linkurl'));
    //window.location.reload();

     reload(function () {
        popup();
    });

  });
});

 function reload(callback) {
      if (document.URL.indexOf("#") == -1) {
    // Set the URL to whatever it was plus "#".
    url = document.URL + "#";
    location = "#";

    //Reload the page
    location.reload(true);
    return true;
      }

    }


function popup() {
      document.getElementById('light').style.display = 'block';
      document.getElementById('fade').style.display = 'block';
    }

答案 2 :(得分:0)

尝试这样的事情:

function reload() {
      if (document.URL.indexOf("#") == -1) {
        // Set the URL to whatever it was plus "#".
        url = document.URL + "#";
        location = "#";

        //Reload the page
        location.reload(true);
        return true;
      }
function popup() {
      document.getElementById('light').style.display = 'block';
      document.getElementById('fade').style.display = 'block';
    }
if($.session.set("reload") == true ) {
  popup();
  $.session.set("reload", false);
}
$('.button').click(function() {
   $.session.set("reload", true);
   reload();
});
相关问题