【问题标题】:firefox javascript window.open _selffirefox javascript window.open _self
【发布时间】:2015-09-19 15:17:55
【问题描述】:

我的问题是:
当我使用时:

window.open("example.com","_self");

self.open("example.com");

window.location.href="example.com";

Firefox 删除所有菜单、按钮、窗口的窗口最小化按钮,一切。上下文菜单也停止工作,但网站打开正常,除了这种混乱,这会破坏一切。

那么如何解决这个问题?

编辑: 我正在使用FF22,全新安装。 看起来这不是一个简单的案例,所以我把整个代码放在这里,它是稍微编辑的插件,用于从上下文菜单创建新选项卡:

let _ = require("l10n").get;
let winUtils = require("window-utils");
let { isBrowser } = require("api-utils/window/utils");

var delegate = {
onTrack: function (window) {
if (isBrowser(window) ){
    let menu = window.document.getElementById("tabContextMenu");
        let newtab = window.document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul","menuitem");
            newtab.setAttribute("id", "contexttab-newtab");
            newtab.setAttribute("label", _("newtab_string"));
            newtab.setAttribute("accesskey", _("newtabaccesskey_string"));
            newtab.setAttribute("oncommand", "window.location.href='http://www.example.com'");
            menu.insertBefore(newtab, menu.firstChild);
        } // End isBrowser
    } // End ontrack
} // End delegate function

let tracker = new winUtils.WindowTracker(delegate);


// code to remove the menuitem when extension is disabled for satisfy requirement on AMO for pass a full review
// On uninstall the menuitem is not removed, see: https://bugzilla.mozilla.org/show_bug.cgi?id=627432

exports.onUnload = function(reason) {
    var unloader = {
        onTrack: function (window) {
            if (isBrowser(window) ){
                let menu = window.document.getElementById("tabContextMenu");
                let newtab = window.document.getElementById("contexttab-newtab");
                menu.removeChild(newtab);
            }
        }
    }; // End unloader function

    let remover = new winUtils.WindowTracker(unloader);
}

这是我编辑的唯一一行:

newtab.setAttribute("oncommand", "window.location.href='http://www.example.com'");

【问题讨论】:

  • 它会直接将您重定向到“example.com”,这就是它不显示您的菜单和所有内容的原因..如果您想在页面的特定位置打开 url,您可以使用框架或有点在特定位置打开网址..
  • 您使用的是哪个版本的 FF?我can't reproduce这个和FF24。
  • 我更新了第一篇文章。
  • 更改 window.location 从来都不是一个好主意,最好的方法是服务器端重定向或使用 ajax 或在框架中加载页面。

标签: javascript firefox firefox-addon


【解决方案1】:
gBrowser.loadURI('http://www.example.com');

正常工作。

【讨论】:

    【解决方案2】:

    gBrowser.loadURI 将页面加载到我认为的选定选项卡中。

    如果你想打开一个新窗口,你必须这样做:

    var url = Cc['@mozilla.org/supports-string;1'].createInstance(Ci.nsISupportsString);
    url.data = 'http://www.bing.com/';
    Services.ww.openWindow(null, 'chrome://browser/content/browser.xul', '_blank', 'chrome,all', url);
    

    【讨论】:

    • OP 不想打开新窗口(因此 OP 的原始调用在 _self 中传递)。
    猜你喜欢
    • 2011-06-16
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-05
    相关资源
    最近更新 更多