2

I have very simple code

$('.opennewwindow').click(function(e) {
    e.preventDefault();
    window.open("http://www.stackoverflow.com")
    /*alert('its works');*/
});

<button class="opennewwindow" value="somevalue">Open new window!</button>

Unfortunately, the code does not work (the function calls, but not open.window). Errors dont appear anywya.

I tried like this too:

$('.opennewwindow').click(function(e) {
    e.preventDefault();
    myWin = window.open("http://www.stackoverflow.com");
    myWin.focus();
});

and following error TypeError: myWin is undefined.

I used a $(document).ready()etc. but same effect.

The clue is that on my website I have a ajax scripts. This button and function is also loaded by ajax.

Anyone know why this is not working?

Adrian Forsius
  • 1,437
  • 2
  • 19
  • 29
user2363971
  • 191
  • 1
  • 4
  • 19

3 Answers3

4

Your first code just works. See http://jsfiddle.net/69a1cL1v/

It is probably a pop-up blocker.

$('.opennewwindow').click(function(e) {
    e.preventDefault();
    window.open("http://www.stackoverflow.com")
    /*alert('its works');*/
});
Adrian Forsius
  • 1,437
  • 2
  • 19
  • 29
TeeDeJee
  • 3,756
  • 1
  • 17
  • 24
  • I thought so at first, but I did a test page (I removed everything except what above) and it works, a window opens ... – user2363971 Sep 10 '14 at 12:39
2

Oh man what I done!

I just (in my custom jquery scripts) override function "open"... I dont know why I did it (it was unused function). I'm sorry for the trouble.

user2363971
  • 191
  • 1
  • 4
  • 19
0

Tools > Options, Content area, "Advanced" button to the right of "Enable JavaScript" should give you the option to allow this behavior. If you're on Linux, the navigation might be slightly different.

Note, it is generally considered best practice to allow the user to choose what window should be in focus.

This is about:config you're referring to: dom.disable_window_flip

In FF I am able to open a new window with focus by providing window name and option parameters to window.open, and then calling focus.

var wi = window.open('http://www.stackoverflow.com', 'window_name', 'height=200,width=200');
wi.focus();

For Chrome support, you have to check out: Google Chrome "window.open" workaround?

Community
  • 1
  • 1
PythonDev
  • 4,287
  • 6
  • 32
  • 39