Unanswered: window.open not working?
Is there a way to get "window.open" working in javascript running in a packaged app?
I know there are possibilities like "Ion.util.openUrl(...)" for Default-Browser-Windows and "Ion.ui.Window({url: _url, title: 'Second Window'})" for Application-Windows. But I have some old views running in my app which use "window.open".
Wrap window.open?
I'm brand new to packager so forgive me if this is ignorant but couldn't you wrap the window.open [1] command?
[1]
Seems like a good and simple solution - Thank you.
Gonna try it as soon as we focus on this project.
Just implemented this for my app:
Code:
//only apply if running in packaged app if(window.Ion){ window.open = function (open) { return function (url, name, features) { //call ion function instead return Ion.util.openUrl(url); }; }(window.open); }
- Ion package returns boolean, normal javascript will return a window object. In my case I didn't care, but if you expect to do anything with the return, this solution won't work
- The only reason I do "if window.Ion" is because I'm continuing to support the standard webapp as well. If you're doing packaged only, this is not necessary (but doesn't hurt).
- I put this in the launch() of my app, but it can be anywhere as long as it's executed before you try to call window.open.
To avoid circular calls, you need to stash away the original window.open function in variable.
A nice way (that doesn't pollute the global namespace) is to use a closure. Pass the original window.open function to an anonymous function as an argument (called open below). This anonymous function is a factory for your hook function. Your hook function is permanently bound to the original window.open function via the open argument:
window.open = function (open) {
return function (url, name, features) {
// set name if missing here
name = name || "default_window_name";
return open.call(window, url, name, features);
};
}(window.open);
Can somebody explain more about this ?NgonQua.Net - Photos Sharing
Break it up a little:
Code:
var openWrapper = function (originalOpen) { console.debug('original open exists here:' + originalOpen); var newOpen = function (url, name, features) { console.debug('original open STILL exists here:' + open);//<-- open is passed in by scope // set name if missing here name = name || "default_window_name"; return originalOpen.call(window, url, name, features); }; return newOpen; };
Then tell it to wrap something:
Code:
var openWrapped = openWrapper(window.open);
Code:
openWrapped('')