获取“appcontent”位置.主机

发布于 2025-01-02 14:54:35 字数 714 浏览 0 评论 0原文

我不是专业的 JavaScript 用户,并且在使用 addEventListener 时遇到困难。

var appcontent = document.getElementById("appcontent");
appcontent.addEventListener("DOMContentLoaded", load, true);

function load(aEvent) {
    var doc = aEvent.originalTarget;
    alert(doc.location.host);
}

在附加组件中,此代码将向 appcontentlocation.host 发出警报。我的问题是,我不需要事件侦听器,并且想像普通函数一样调用 load:

var appcontent = document.getElementById("appcontent");
load(appcontent);

function load(aEvent) {
    var doc = aEvent.originalTarget;
    alert(doc.location.host);
}

这就是我试图做的,但它不起作用。

I am not an expert JavaScript user and I am having difficulty with addEventListener.

var appcontent = document.getElementById("appcontent");
appcontent.addEventListener("DOMContentLoaded", load, true);

function load(aEvent) {
    var doc = aEvent.originalTarget;
    alert(doc.location.host);
}

In an add-on this code will alert the location.host of the appcontent. My problem is that I don't need an event listener and want to call load like a normal function:

var appcontent = document.getElementById("appcontent");
load(appcontent);

function load(aEvent) {
    var doc = aEvent.originalTarget;
    alert(doc.location.host);
}

This is what I was trying to do but it doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹂绝世的画 2025-01-09 14:54:35

您的 load() 函数仍然需要一个事件,但您现在将实际元素传递给它。另外,您想知道当前所选选项卡的哪个位置?然后您可以使用全局 content 变量,它指向当前选项卡的 window 对象。因此,将 load() 函数更改为类似以下内容应该可行:

function load() {
    alert(content.location.host);
}

使用 Ctrl+Shift+J 打开 JavaScript 控制台并检查扩展程序的错误 - 它应该可以帮助您找到问题所在。

Your load() function still expects an event but you are now passing the actual element to it. Also, which location do you want to know, that of the currently selected tab? Then you can use the global content variable, it is pointing to the window object of the current tab. So changing the load() function into something like this should work:

function load() {
    alert(content.location.host);
}

Use Ctrl+Shift+J to open JavaScript Console and to check the errors for your extension - it should help you find our what the issue is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文