cancel
Showing results for 
Search instead for 
Did you mean: 

run code "window.open(url,...)" in javascript, the action is different according to the time this line of code is executed.

SOLVED
Highlighted
New Contributor

run code "window.open(url,...)" in javascript, the action is different according to the time this line of code is executed.

Hi FireFox support, We are developing a web application with javascript, we use firefox as our default browser for our users.

Now we met a problem when we use window.open in our application.

In our application, we need to open new page in a new browser tab, we used code : window.open('http://ourUrl.com', '_blank');

The action of the browser is different according when this line of code is executed.

situation1: user clicked a button, the click event will trigger our js function, in this function, execute this line of code directly, then the browser may open the page in a new browser tab. (this is what we wanted.)

situation2: user clicked a button, we firstly submit the data on page to our server, and in the callback function, we execute this line of code, it may pop an independent new window without tool bar.(this is not what we wanted).

I don't understand why the action of browser is different, can you help to explain? Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Legendary Contributor

Re: run code "window.open(url,...)" in javascript, the action is different according to the time this line of code is executed.

When the thread started, were you really running Firefox 11? Perhaps it changed between Firefox 11 (March 2012) and Firefox 19 (March 2013). You could install an old version if you were really curious. To avoid disturbing your main installation, you can use the unofficial "Portable" edition for testing. http://sourceforge.net/projects/portableapps/files/Mozilla%20Firefox%2C%20Portable%20Ed./

18 REPLIES
Legendary Contributor

Re: run code "window.open(url,...)" in javascript, the action is different according to the time this line of code is executed.

Assuming you are not navigating away from the page, I would expect the same exact results from window.open() in both situations.

https://developer.mozilla.org/en-US/docs/DOM/window.open

Is it possible to post a test case demonstrating the problem?

Legendary Contributor

Re: run code "window.open(url,...)" in javascript, the action is different according to the time this line of code is executed.

Also, I don't know why you are posting with Firefox 11, but make sure you test with Firefox 19.0.2 in case this is a solved bug.

New Contributor

Re: run code "window.open(url,...)" in javascript, the action is different according to the time this line of code is executed.

Hi jscher20000, the case used in our project is complex, so I made a test, there is no way to attach the attachment here, so I have to pasted the code in the end

I used requirejs to require a js file, and in the callback function I called function window.open(url,'_blank')

I tested them in different browsers, In firefox, click the button 'indirectly' it will open this window in a new tab.

in Chrome, click button "indirectly", it will open a new window instead of a new tab. but if click button "directly" it will open a new tab.

I just don't understand when browser will open a new window, and when opens new tab.


the handler of the "indirectly" and "directly" buttons is written as follows:

clickIndirectly=function(){
   require(['./js/testwindowopen/in.js'],function(){
   window.open('http://google.com','_blank');
  })
}

clickDirectly = function(){
   window.open('http://google.com','_blank');
}
Legendary Contributor

Re: run code "window.open(url,...)" in javascript, the action is different according to the time this line of code is executed.

What does require() do with the callback function that is different from straight JavaScript?

New Contributor

Re: run code "window.open(url,...)" in javascript, the action is different according to the time this line of code is executed.

require is a javascript framework, in the function:

clickIndirectly=function(){

  require(['./js/testwindowopen/in.js'],function(){
        window.open('http://google.com','_blank');
 })

}

when the function is called, browser will get a javascript file from server via url "./js/testwindowopen/in.js" after the js is loaded ,the call back function :

     function(){
            window.open('http://google.com','_blank');
     }

will be called.

Legendary Contributor

Re: run code "window.open(url,...)" in javascript, the action is different according to the time this line of code is executed.

I see that part, but requireJS appears to be executing the function in a different way. Does your JS library replace the standard window.open function? Is there a change in context such as window referring to an iframe?

New Contributor

Re: run code "window.open(url,...)" in javascript, the action is different according to the time this line of code is executed.

Hi jscher20000,

I checked the source code of requireJS and our own code, there is no replacement of the standard window.open function, and there is no change of context by our code such as using iframe. the only difference is the place where window.open is called.

Is there any security consideration of the browser when open a link by JS? Or the context is different between the two situations?

Legendary Contributor

Re: run code "window.open(url,...)" in javascript, the action is different according to the time this line of code is executed.

What if you change it to something like this:

clickIndirectly=function(){
  require(['./js/testwindowopen/in.js'],function(){
    clickDirectly;
  })
}
New Contributor

Re: run code "window.open(url,...)" in javascript, the action is different according to the time this line of code is executed.

it still opens the link in new window.