简体   繁体   English

使用JavaScript打开动态链接新窗口(但不弹出)

[英]Opening dynamic link new window (but not popup) with JavaScript

I am trying to open a link in new window, however the link may contain an additional QueryString which is unfortunately available when the user clicks the link. 我正在尝试在新窗口中打开链接,但是该链接可能包含一个附加的QueryString,不幸的是,当用户单击该链接时,该字符串才可用。 So even the link is given in the code like: 因此,即使链接是在代码中给出的,例如:

<a href="somepage.aspx">click</a>

when the user clicks I want user to go to 当用户单击时,我希望用户转到

<a href="somepage.aspx?id=1">click</a>

and the "1" value on the above example is calculated when user clicks the link. 当用户单击链接时,将计算出上面示例中的“ 1”值。 I am aware I can accomplish this with 我知道我可以做到这一点

<script>
function openlink()
{
   var calc_i;
   // do calculations and update calc_i value
   window.open("somepage.aspx?id="+calc_i);
}
</script>
<a onclick="openlink()">click</a>

This results in a popup window (which I don't want). 这将导致一个弹出窗口(我不想要)。 What is your suggestion? 你有什么建议?

It really would be better to just have the querystring in the link already, it would make far more sense. 确实已经在链接中包含querystring会更好,这将更加有意义。

You could try appending the code in an onclick event like so. 您可以尝试像这样在onclick事件中附加代码。

<a href="somepage.aspx" onclick="openlink(this)" target="_blank">Hello</a>

function openlink(anchor){
    var calc_i = 1;
    var page = anchor.getAttribute('href');
    anchor.setAttribute('href',page + '?id=' + calc_i);
}

Try the following: 请尝试以下操作:

function openlink()
{
    var calc_i;
    // do calculations and update calc_i value
    window.open("somepage.aspx?id="+calc_i, "_blank");
}

You can specify the name of the new window, which should be opened in window.open. 您可以指定新窗口的名称,该名称应在window.open中打开。 If you use _blank it opens up in a complete new window. 如果使用_blank它将在一个全新的窗口中打开。

Working example 工作实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM