如何将此代码添加到我已经拥有的代码中以使此代码生效

时间:2017-07-31 15:39:12

标签: javascript google-chrome

我想将第二个代码实现到第一个代码中,以使其按预期工作。我不确定该怎么做 目前的代码:

<script type="text/javascript">
  !function(){
    var campaign_link = ""; // REPLACE WITH YOUR LINK
    var t;             
    try{
      for(t=0;10>t;++t)history.pushState({},"","#");
      onpopstate=function(t){t.state&&location.replace(campaign_link + window.location.search.substring(1))}}
    catch(o){}
  }();
</script>

我想要添加到现有代码的代码;

window.addEventListener("popstate", function(e) {
    window.location.href = location.href;
});

1 个答案:

答案 0 :(得分:0)

您可以将此功能添加到当前脚本中。但是,您可能希望将loction.href存储在var中而不是window.location

<script type="text/javascript">
  function(){
    var campaign_link = ""; // REPLACE WITH YOUR LINK
    var t;             
    try{
      for(t=0;10>t;++t)history.pushState({},"","#");
      onpopstate=function(t){t.state&&location.replace(campaign_link + window.location.search.substring(1))}}
    catch(o){}
  }();

  window.addEventListener("popstate", function(e) {
    window.location.href = location.href;
  });

</script>