在两个窗口中都打开页面

page opening in both window

本文关键字:窗口 两个      更新时间:2023-09-26

我的页面中有一个提交按钮,我想在不同的窗口中打开该链接。

检查这个代码

  <input class="btn add-to-cart-btn" onclick="one();two();" type="submit" value="More Info At {{ product.vendor }}"/>

java脚本代码

<script>
  function one(){
   trackOutboundLink('{{ product.metafields.google.custom_label_0 }}');

}
function two(){
    window.open('{{ product.metafields.google.custom_label_0 }}');
}

  var trackOutboundLink = function(url) {
    ga('send', 'event', 'outgoing', 'click', url, {
    'transport': 'beacon',
    'hitCallback': function(){document.location = url;}
  });
    }
</script>

"product.metafields.google.custom_label_0"是一个静态url。

若我点击那个提交页面,那个么那个链接就会在新窗口和同一窗口中打开。

但我只想在不同的窗口打开。如何控制?

实际问题在于:-

'hitCallback': function(){document.location = url;}

在按钮上,首先单击one();,然后调用two();函数。因此,在再次打开新窗口后,函数one(); 中调用的函数trackOutboundLink()的上述代码重新加载的原始URL

所以简单地删除它或评论如下:-

<input class="btn add-to-cart-btn" onclick="one();two();" type="submit" value="More Info At {{ product.vendor }}"/>
<script>
  function one(){
   trackOutboundLink('{{ product.metafields.google.custom_label_0 }}');
}
function two(){
    window.open('{{ product.metafields.google.custom_label_0 }}');
}
var trackOutboundLink = function(url) {
    ga('send', 'event', 'outgoing', 'click', url, {
    'transport': 'beacon',
    /* 'hitCallback': function(){document.location = url;} */
  });
}
</script>

来源:http://www.w3schools.com/jsref/met_win_open.asp

您可以尝试:var myWindow = window.open("", "", "width=200,height=100");在新窗口中打开关于:空白页。

你必须设置窗口大小。

将输入类型从"提交"更改为"按钮"

语法

window.open(URL,name,specs,replace)

试试这个

window.open("product.metafields.google.custom_label_0 ", "_blank");

更多信息