确定iframe元素中的现有属性

时间:2015-06-23 18:29:39

标签: javascript dom iframe

以下脚本:

function getIframe(id) {
   var iframe=null;
   for (var i = window.frames.length - 1; i >= 0; i--) {
      if(window.frames[i].frameElement.id==id){iframe=window.frames[i];break;}
   }
   return iframe;
}

console.log('iframe:',iframe);
console.log('keys',Object.keys(iframe));
console.log('iframe["width"]',iframe["width"]);

产生以下结果:

iframe: Window ~.php?task=get_image&dummy=1435083899977
keys ["close", "stop", "focus", "blur", "open", "alert", "confirm", "prompt", "print", "showModalDialog", "postMessage", "captureEvents", "releaseEvents", "getSelection", "getComputedStyle", "matchMedia", "moveTo", "moveBy", "resizeTo", "resizeBy", "scroll", "scrollTo", "scrollBy", "requestAnimationFrame", "cancelAnimationFrame", "getDefaultComputedStyle", "mozRequestAnimationFrame", "mozCancelAnimationFrame", "mozCancelRequestAnimationFrame", "scrollByLines", "scrollByPages", "sizeToContent", "updateCommands", "find", "dump", "setResizable", "btoa", "atob", "setTimeout", "clearTimeout", "setInterval", "clearInterval", "self", "name", "history", "locationbar", "menubar", "personalbar", "scrollbars", "statusbar", "toolbar", "status", "closed", "frames", "length", "opener", "parent", "frameElement", "navigator", "external", "applicationCache", "screen", "innerWidth", "innerHeight", "scrollX", "pageXOffset", "scrollY", "pageYOffset", "screenX", "screenY", "outerWidth", "outerHeight", "performance", "mozAnimationStartTime", "mozInnerScreenX", "mozInnerScreenY", "devicePixelRatio", "scrollMaxX", "scrollMaxY", "fullScreen", "mozPaintCount", "onwheel", "ondevicemotion", "ondeviceorientation", "ondeviceproximity", "onuserproximity", "ondevicelight", "content", "console", "sidebar", "crypto", "onabort", "onblur", "onfocus", "oncanplay", "oncanplaythrough", "onchange", "onclick", "oncontextmenu", "ondblclick", "ondrag", "ondragend", "ondragenter", "ondragleave", "ondragover", "ondragstart", "ondrop", "ondurationchange", "onemptied", "onended", "oninput", "oninvalid", "onkeydown", "onkeypress", "onkeyup", "onload", "onloadeddata", "onloadedmetadata", "onloadstart", "onmousedown", "onmouseenter", "onmouseleave", "onmousemove", "onmouseout", "onmouseover", "onmouseup", "onpause", "onplay", "onplaying", "onprogress", "onratechange", "onreset", "onresize", "onscroll", "onseeked", "onseeking", "onselect", "onshow", "onstalled", "onsubmit", "onsuspend", "ontimeupdate", "onvolumechange", "onwaiting", "onmozfullscreenchange", "onmozfullscreenerror", "onmozpointerlockchange", "onmozpointerlockerror", "indexedDB", "onerror", "onafterprint", "onbeforeprint", "onbeforeunload", "onhashchange", "onlanguagechange", "onmessage", "onoffline", "ononline", "onpagehide", "onpageshow", "onpopstate", "onunload", "localStorage", "sessionStorage", "window", "document", "location", "top", "$", "jQuery", "jQuery19107258252193071153", "obj", "virtualDirectory", "formatSizeUnits", "getThumb_folder", "getThumb_file", "getList_folder", "getList_file", "InstallTrigger"]
iframe["width"] <input id="width" class="input-small numbers-only" type="text" value="" title="Width" placeholder="Width">

如图所示,“width”不是iframe的关键,当点击firebug中的Window ~.php?task=get_image&dummy=1435083124957时,Firebug也不显示“宽度”。

如果不随意尝试不同的字符串,例如“width”,我如何确定iframe元素中的现有属性?

1 个答案:

答案 0 :(得分:0)

来自Window.frames&amp;&amp; iframe,这些是不同的,你得到的是window.frame对象,就像子窗口一样,你可以使用innerWidth来获取其视口的宽度。如果你使用类似的东西 document.getElementsByTagName("iframe")[0];您将获得<iFrame>这是一个dom元素,因此您可以使用类似width的内容来访问其属性,就像其他dom元素一样。

我不确定您打算使用哪个对象,但似乎想要更改iframe的宽度,也许您应该使用dom一个?

经过一些小测试,iframe['width']将有价值,将有两种可能的情况:

  1. 当前域中的某些js代码将值分配给iframe['width']
  2. iframe src页面&#39; js为其window.width
  3. 分配值

    但是这两种方法都会使Object.keys(iframe)具有键width

    所以,有些人可能会这样做

      当前窗口中的
    1. Object.getPrototypeOf(iframe).width = .....;
    2. iframe&#39; src页面中的
    3. Object.getPrototypeOf(window).width = .....;
    4. 我不确定为什么会有人这样做,但在这种情况下,也许您可​​以访问Object.getPrototypeOf(iframe)以获取所有可能的密钥。