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.
Valgt løsning
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./
Læs dette svar i sammenhæng 👍 0Alle svar (18)
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?
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.
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'); }
Ændret af cor-el den
What does require() do with the callback function that is different from straight JavaScript?
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.
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?
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?
What if you change it to something like this:
clickIndirectly=function(){ require(['./js/testwindowopen/in.js'],function(){ clickDirectly; }) }
it still opens the link in new window.
it still opens the link in new window.
Yes, that is expected because the target of window.open() is _blank. It still has different toolbars?
Hi jscher20000,
What I mean is that it opens the new page in new window, the window has no tool bar. What I expect is open the window in a new browser tab.
Sorry, but I think you need someone who understands the requirejs library to explain why window.open() works differently when called from the callback.
Thanks jscher20000,
this problem has no relationship with require js, I used require js just as a example,
before we involve requirejs in our project, we submit data to server by AJAX, and in the callback function, we use window.open, the browser will open new window too.
Okay, that is simpler to understand. Your script submits data to a server and does something with the response and then uses window.open(). What result do you get in my test page?
Hi jscher20000,
Open your test page in firefox, both links opens the new page in new browser tabs, it seems firefox has fixed this problem, I tested in our own application, it also acts as we expected.
But in chrome, it still opens new window, it seems to be a bug of chrome, I will report this bug to them.
Thank you for your reply and your time for supporting this issue.
Really, after all that, it's a Chrome problem?!
It appears you're out of luck in Chrome unless something has changed recently:
http://stackoverflow.com/questions/11999837/force-window-open-to-create-new-tab-in-chrome
http://stackoverflow.com/questions/9864747/new-window-doesnt-open-as-tab-in-chrome
Thanks jscher20000,
Is this problem fixed in firefox recently? I remember that I met it recently, but I can not reproduce it now.
Valgt løsning
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./