javascript - 获取所有用户定义的窗口属性?

标签 javascript browser properties window-object

有没有办法在 javascript 中找出所有用户定义的窗口属性和变量(全局变量)?

我尝试了 console.log(window) 但列表是无穷无尽的。

最佳答案

您还可以将窗口与干净版本的窗口进行比较,而不是尝试在运行时进行快照以进行比较。我在控制台中运行了它,但是您可以将它变成一个函数。

// make sure it doesn't count my own properties
(function () {
    var results, currentWindow,
    // create an iframe and append to body to load a clean window object
    iframe = document.createElement('iframe');
    iframe.style.display = 'none';
    document.body.appendChild(iframe);
    // get the current list of properties on window
    currentWindow = Object.getOwnPropertyNames(window);
    // filter the list against the properties that exist in the clean window
    results = currentWindow.filter(function(prop) {
        return !iframe.contentWindow.hasOwnProperty(prop);
    });
    // log an array of properties that are different
    console.log(results);
    document.body.removeChild(iframe);
}());

关于javascript - 获取所有用户定义的窗口属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24820423/

相关文章:

spring - 使用Spring加载属性(通过系统属性)

javascript - 组件中每个元素的不同 handleChange() 函数? ( react

javascript - 如何在php中使用json检索图像

asp.net - 与 GET/POST 请求有些混淆

javascript - 不同机器上的两个浏览器查看同一个页面

properties - 不同类型的属性 setter

javascript - 如何向 chart.js 折线图添加点?

javascript - 在 div 元素中调用 JQuery 函数

image - 下载的页面大小与浏览的页面大小

objective-c - Objective-C - 如果没有什么不同,则使用访问器