Reputation: 8574
So on my MVC site, I need to open a dynamically generated URL in a new tab. What I've got for code so far is below:
<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("/");
}
Now, the URL has to be generated when the link as clicked; as it will go to an external payment gateway and the authorization time is extremely short-lived. Right now the code works; where it will open up the page correctly, but not in a new tab. If i try adding in target="blank" href="/"
to the anchor tag; i end up getting the landing page in a new window and the payment gateway in the original.
How can i get this to pop in a new window?
Upvotes: 0
Views: 1325
Reputation: 90
JavaScript: location.href to open in new window/tab? I guess here is the answer. Since you are trying to load page using script, target attribute is ignored. You have to specify it for the script that you want to open a new page. Something like this:
<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("/");
}
might work
Upvotes: 1
Reputation: 78
Have you tried using window.open instead?
window.open("http://www.google.com")
~A
Upvotes: 0