0

I want to open a url in new tab.

I am using following code to open url in new tab.

var url = "/Billing/DownloadEDIForMultipleClaim?month=" + month + "&BillRequestIds=" + billRequestIds.join(',') + "&PatientIDs=" + patientIds.join(',');
window.open(url, '_blank'); //exception here

But this is not working for me it through an exception Uncaught TypeError: Cannot read property 'open' of undefined

I also used following.

var url = "/Billing/DownloadEDIForMultipleClaim?month=" + month + "&BillRequestIds=" + billRequestIds.join(',') + "&PatientIDs=" + patientIds.join(',');
var win = window.open(url, '_blank');
if(win) {
    //Browser has allowed it to be opened
    win.focus();
} else {
    //Broswer has blocked it
    alert('Please allow popups for this site');
}

I already seen related questions.

How to open a URL in a new Tab using javascript or jquery?

How to open a link in new tab using javascript

But this is not working for me.

Community
  • 1
  • 1
Manoj
  • 4,951
  • 2
  • 30
  • 56
  • 2
    So basically what the message is saying is that `window` is undefined, which seems highly unlikely ? – adeneo Jan 10 '15 at 05:37
  • `open is undefined' when I use 'window.location.href' then exception occur `location is undefined` – Manoj Jan 10 '15 at 05:41
  • In the standard implementation of javascript inside a browser the window object should be defined to the current view surface. So you might have an browser issue or your code might have overloaded this object. – Daniel Persson Jan 10 '15 at 05:44
  • @DanielPersson I doubt about overloaded `window` object since that would not give this error `Uncaught TypeError: Cannot read property 'open' of undefined`. – vjdhama Jan 10 '15 at 05:46
  • Ok overloaded was the wrong wording :). Changed, overwritten or not defined. – Daniel Persson Jan 10 '15 at 05:50
  • @DanielPersson the global window object is read-only. Of course, one could define a local window variable, and that's probably what the issue is, something like this -> **http://jsfiddle.net/jwzaydrL/** – adeneo Jan 10 '15 at 05:53
  • http://jsfiddle.net/sc3ownLp/ Check this out. You will need to unblock the pop-up. – Hozikimaru Jan 10 '15 at 05:54
  • @SurgeonofDeath - That's not the same error message, you've just mispelled `Open` ! – adeneo Jan 10 '15 at 05:57
  • @adeneo yeah, I forgot to fix that. It works now. – Hozikimaru Jan 10 '15 at 05:59
  • @SurgeonofDeath - well, yeah! `window.open` works for everyone, just not the OP apparantly, and showing that it works in a fiddle doesn't really help the OP at all? – adeneo Jan 10 '15 at 06:02
  • @adeneo That is exactly why I indicated to the pop up. I do not see anyone has mentioned about this. Unless you allow the pop-up, it will not work and that could very well be the case. That is why, it clearly says you will need to unblock the pop-up. – Hozikimaru Jan 10 '15 at 06:04
  • What browser are you using? Is there other code around it? Show the rest of the code. – epascarello Jan 10 '15 at 06:08

1 Answers1

0

This may be of use: http://jsfiddle.net/vraCM/12/ it seems that this code snippet contains what you need for this to work.

$(function() {
   $("a[href^='http']:not([href*='jsfiddle.net'])").each(function() {
       $(this).click(function(event) {
             event.preventDefault();
             event.stopPropagation();
             window.open(this.href, '_blank');
        }).addClass('externalLink');
   });
});

The js fiddle contains code that can be manipulated to fit your parameters regarding pop-ups within an alert or new window etc.

Also, this article may be worth a read. http://www.jqueryfaqs.com/Articles/Open-new-tab-in-browser-using-jQuery.aspx

Ballard
  • 869
  • 11
  • 25