在新选项卡中打开动态生成的链接
问题描述:
因此,在我的MVC网站上,我需要在新选项卡中打开动态生成的URL。到目前为止我得到的代码如下:在新选项卡中打开动态生成的链接
<a class='noline' onclick="location.href='<%:@Url.Action("GeneratePaymentUrl", "Home", new {target="_blank"})%>'"> Pay Invoices</a>
public ActionResult GeneratePaymentUrl()
{
try
{
string returl = GetPaymentUrl();
if (returl.ToLower().StartsWith("http"))
{
//Response.Redirect(returl);
return new RedirectResult(returl, false);
}
else
{
return new RedirectResult("/");
}
}
catch (Exception Ex)
{
}
return new RedirectResult("/");
}
现在,必须在点击链接时生成URL;因为它会转到外部支付网关,授权时间极短。现在代码有效;它将在哪里打开正确的页面,但不是在新标签中。如果我尝试将target="blank" href="/"
添加到锚标记中;我最终在新窗口中获取了登录页面,并在原始页面中显示了付款网关。
我如何得到这个在新窗口弹出?
答
JavaScript: location.href to open in new window/tab?我想这里就是答案。由于您正尝试使用脚本加载页面,因此目标属性将被忽略。您必须为要打开新页面的脚本指定它。事情是这样的:
<a class='noline' onclick="window.open('<%:@Url.Action("GeneratePaymentUrl", "Home", new {target="_blank"})%>,'_blank'
);"> Pay Invoices</a>
public ActionResult GeneratePaymentUrl()
{
try
{
string returl = GetPaymentUrl();
if (returl.ToLower().StartsWith("http"))
{
//Response.Redirect(returl);
return new RedirectResult(returl, false);
}
else
{
return new RedirectResult("/");
}
}
catch (Exception Ex)
{
}
return new RedirectResult("/");
}
可能工作
答
您是否尝试过使用window.open?
window.open("http://www.google.com")
〜一
没错;而已。使用指定的_blank打开window.open。早晨在咖啡前张贴我的作品。谢谢! –
不用担心吉姆! –