Get user defined window global variables with javascript

This snippet will return user defined properties on the window object.

This is useful for seeing what globals are being exposed to the window.

It works by creating an iframe and comparing the window element of the empty iframe.

function getWindowVars() {
  // create an iframe and append to body to load a clean window object
  const iframe = document.createElement('iframe')
  // hide iframe = 'none'
  // add iframe to page

  // get the current list of properties on window
  const currentWindow = Object.getOwnPropertyNames(window)

  // filter the list against the properties that exist in the clean window
  const results = currentWindow.filter(function(prop) {
    return !iframe.contentWindow.hasOwnProperty(prop);

  // Clean up iframe

  return results

// usage:
const userDefinedVars = getWindowVars()

For example you might want to see if browser extensions are polluting the global window or what analytics tools a site has installed.