-2

I'm trying to get a link in javascript to open a url in a new tab. I've found a number of posts for target="blank" using attribute and a couple other ways but can't seem to get it to work. Basically, if v_virt = "invoices" I just need the url to open in a new tab. Does anyone know the proper syntax?

if(v_virt=="invoices"){
location.href=('https://www.example.com/invoices/invoice?ProjectID=[@field:ProjectID]&InvoiceID=[@field:InvoiceID]', '_blank');
}
Kevin L
  • 29
  • 1
  • 7

1 Answers1

1

You have to use window.open() rather than location.href.

  • location.href changes the URL of the current page;

  • window.open() opens a new pop-up window navigated to the specified page.

For example:

window.open('https://www.example.com/invoices/invoice?ProjectID=[@field:ProjectID]&InvoiceID=[@field:InvoiceID]');
Toastrackenigma
  • 7,604
  • 4
  • 45
  • 55
  • Is there any way to not use a popup? A lot of my users have popup blockers and will get confused if they have to ok it. – Kevin L Aug 03 '17 at 19:12
  • Also, thanks for clarifying the difference between location.href and window.open. I wasn't quite clear on that. – Kevin L Aug 03 '17 at 19:13
  • I see the link above with a similar issue re tabs vs popup. The odd thing is I have my test browser (chrome) set up to open new windows in tabs but this code forces a popup. – Kevin L Aug 03 '17 at 19:15
  • @KevinL Opening a new tab / window technically is a popup, which is sad in a way as sometimes there are legitimate use cases for it. You could always do something like this if it is a large problem for you: https://jsfiddle.net/mbqvjhpw/ – Toastrackenigma Aug 03 '17 at 19:32
  • thanks for the link & explanation- really helps – Kevin L Aug 04 '17 at 20:28