【问题标题】:Javascript behavior between 2 different functions that I don't understand我不理解的 2 个不同函数之间的 Javascript 行为
【发布时间】:2019-09-26 07:28:27
【问题描述】:

我是一名 Java 开发人员,不精通前端技术,所以我希望这个问题不要太愚蠢。我在一个 html 页面上有 2 个内联脚本。

<script type="text/javascript">
        function printReceipt(orderId,email) {
            var printWindowSettings;
            var browserUserAgent = navigator.userAgent;

            if (browserUserAgent.indexOf("Chrome") > -1) {
                printWindowSettings = "status=0,toolbar=0,menubar=0,height=500,width=1000,scrollbars=1";
            } else {
                printWindowSettings = "status=0,toolbar=0,location=0,menubar=0,height=500,width=1000,scrollbars=1,noopener=1";
            }

            var path = "/shop/printReceipt?orderid="+orderId;
            if (email!=null)
                path+="&email=" + email; 
            var docPrint = window.open(path, '_blank', printWindowSettings);
            if (docPrint == null) console.log("window open returned null");
        }
    </script>

    <script type="text/javascript">
        bajb_backdetect.OnBack = function()
        {
            window.history.back=function(){
              console.log("Back Button Pressed.")
              document.location='/shop/shoppingCart.seam';
            }
        }
    </script>

printReceipt() 在锚标记的onClick() 处理程序中调用。

<div class="pull-left" style="padding-bottom:20px;"><a href="#" onclick="javascript:printReceipt(apaOrder.orderId);" class="btn btn-primary-custom"><i class="fa fa-print" aria-hidden="true" style="padding-right:6px;"></i>Print Receipt</a></div>

我发现当printReceipt() 被调用时,以下脚本(用于管理后退按钮)也会被调用。所以当printReceipt()被调用时,我的浏览器会导航到/shop/shoppingCart.seam

为什么会这样?我该如何解决这个问题?

【问题讨论】:

  • printReceipt 是如何被调用的?你还没有表现出来
  • 我已经更新了我的问题,谢谢。
  • 尝试将“后退按钮”的事件监听器设置为null
  • 你能告诉我那是什么样的吗?我真的不知道你在说什么,对不起。
  • 你说的是哪个按钮?你是说你知道点击锚标签时调用的是什么函数,你能提供一个上下文吗?

标签: javascript


【解决方案1】:

我对您的问题进行了一些研究,由于您提到您使用的是第三方脚本(这对开发人员来说并不总是最好的),我发现了一些可以帮助您摆脱它的方法。

此方法需要您删除已有的第三方脚本(或对其进行评论)。因为我们将以不同的方式处理后退按钮。

script 标记中,您有以下代码:

bajb_backdetect.OnBack = function() {
    window.history.back = function() {
        console.log("Back Button Pressed.")
        document.location = '/shop/shoppingCart.seam';
    }
}

用以下代码替换它:

(function(window, location) {
history.replaceState(null, document.title, location.pathname+"#!/history");
history.pushState(null, document.title, location.pathname);

window.addEventListener("popstate", function() {
  if(location.hash === "#!/history") {
    history.replaceState(null, document.title, location.pathname);
    setTimeout(function(){
      location.replace("/shop/shoppingCart.seam"); //Here goes your URL 
    },0);
  }
}, false);
}(window, location));

现在,如果您确实删除了第三方脚本(或对其进行了评论),您应该能够看到预期的行为。

如果您想了解有关此的更多信息,已经回答了一个与此类似的问题,说处理窗口返回事件的最佳方法是自己完成。

我从this answer 获取了这些信息,只是用针对您的具体问题的解释和代码对其进行了补充。希望对您有所帮助。

注意:如果它仍然无法正常工作,您需要提供更开放的代码上下文,因为可能有其他原因导致它无法正常工作。

【讨论】:

  • 谢谢!但是我要替换代码的哪一部分?我是否保留bajb_backdetect.OnBack =
  • 不,删除该脚本标记中的整个 JavaScript 代码并将代码放在上面。
  • 有效!有用!!非常感谢您对我的耐心。
  • @user1660256 如果我真的帮助了你,点赞并接受答案就足够了我的朋友,它对我的​​帮助比你想象的要多!:)
【解决方案2】:

可能单击的锚点会导致浏览器跟随链接。

鉴于此:

<a href="" onclick="printReceipt(10, 'person@site.com')">click me</a>

单击该链接将运行该功能,然后页面将重新加载。您的背部检测脚本会注意到页面正在被卸载并做一些事情(显然)。

改成:

<a href="" onclick="printReceipt(10, 'person@site.com'); return false">click me</a>

为了防止链接被跟踪。

【讨论】:

  • 我在您的编辑中看到您的 href 是 #,这应该会阻止您的浏览器离开该页面。无论如何都尝试将return false 添加到onlick 中,看看是否有帮助。否则我认为我们需要更多的上下文来解决这个问题。
  • 没有等号,只有return false。我相信,在您的第 3 方脚本完好无损的情况下,它也会起作用。它将防止您在单击链接时破坏您的状态。
  • 嗯,我认为这是一个错字。我没有在我的代码中输入等号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-31
  • 1970-01-01
  • 1970-01-01
  • 2019-10-06
  • 1970-01-01
  • 2019-07-11
  • 2021-02-07
相关资源
最近更新 更多