覆盖window.open停止href工作 [英] overriding window.open stops href to work

查看:58
本文介绍了覆盖window.open停止href工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);



我不知道为什么这段代码会停止href工作。


I dont know why this code stops href to work.

推荐答案

在JavaScript中,没有覆盖这样的概念。这不是面向对象的语言。难怪,如果你不明白你在做什么,有些东西可能不会像幻想所暗示的那样起作用。 :-)



但是,是的,赋值给函数对象 window.open 在某些方面类似于覆盖 。但是 window.open = 之后的代码充斥着废话。你创建它足以看到第一个问题。从本质上讲,这个功能什么都不做。您在处理程序中创建了一些其他函数对象,并且由于一些奇怪的原因,从外部函数返回此对象。您没有显示任何调用 window.open 的代码。您没有显示任何调用返回的内部函数的代码。这不是原始 window.open 函数的工作原理。它应该实际创建一个浏览器窗口的新实例并显示它,而不是返回一个永远不会被调用的函数对象。它应该返回对新的窗口对象的引用,而不是函数对象:https://developer.mozilla.org/en-US/docs/Web/API/Window/open [ ^ ]。



请不要问怎么办?只有当你解释你想要达到的目的时,你才可以获得更多的帮助。



-SA
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


这篇关于覆盖window.open停止href工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆