Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
JavaScript
function targetWindow(name) {
    if(isMobile()){
        return '_self';
    }
    return name;
}

function isMobile() {
    var fnIsIpadNative = window['isMobileNative'];
    return fnIsIpadNative !== undefined && fnIsIpadNative();
}

window.open = function (open) {
    return function (url, name, features) {
        name = targetWindow(name);
        return open.call(window, url, name, features);
    };
}(window.open);

I dont know why this code stops href to work.
Posted
Comments
Sergey Alexandrovich Kryukov 26-May-15 9:41am    
How do you think it's supposed to work? What did you want to achieve?
—SA

1 solution

In JavaScript, there is no such concept as "overriding". This is not an object-oriented language. No wonder, if you don't understand what you are doing, something may not work the way you illusions suggest you. :-)

But yes, assignment to the function object window.open resembles "overriding" in some aspects. But the code after "window.open =" is full of nonsense. You create It's enough to look at the first problem. Essentially, this function does nothing. You create some other function object inside the handler and, by some weird reason, return this object from the outer function. You don't show any code calling window.open. You don't show any code calling the returned inner function. This is not how original window.open function works. It is supposed to actually create a new instance of a browser window and show it, not to return a function object which is never called. It is supposed to return the reference to a new window object, not to the function object: https://developer.mozilla.org/en-US/docs/Web/API/Window/open[^].

Please don't ask "what to do?" You may get some more help only if you explain what did you want to achieve.

—SA
 
Share this answer
 
Comments
markand911 27-May-15 2:52am    
SA, It feels that my question did not portray a clear picture to you. Anyway, I have a single code base which works for mobile and desktop. In desktop every link should open in new tab and in mobile every link should open in same tab. To do this i override the window.open method. I hope this will clear you confusion and you can answer my question. And I am not concerned about the implementation. I am concerned about the mysterious behaviour of href when window.open is overridden.
Sergey Alexandrovich Kryukov 27-May-15 9:07am    
This is a wrong approach, by one simple reason: window.open always creates a new browser window or a tab. The choice depends on parameters and the browser; there is no reliable way to enforce one particular way, because this API was created before tabbed style was uses on browser. More importantly, window.open never shows the same window or anything in the same window. Instead, you have to use location or navigate.

You should not use window.open at all and probably you can embrace the single-window application style.

As to your question and your code, I already answered.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900