Javascript image width and height undefined values -
i try width , heigh previewframe , set values 100% both when try alert undefined values. here html file:
<html> <head> <link rel="stylesheet" href="frame.css"> </head> <body> <div id="previewwrapper"> <div id="previewframe"> <img id="previewimage" src="http://i.forbesimg.com/media/lists/companies/google_416x416.jpg"> </img> </div> </div> <script src="frame.js"></script> </body>
and here css:
html, body{ margin:0; background:#ccc; } #previewwrapper{ margin:44px 0px; width:100%; height:calc(100% - 88px); } #previewframe{ width:100%; height:100%; margin-top:44px; background:#000; } #previewframe img{ max-width:100%; max-height:100%; }
and js:
function imageinfo() { var pframe = document.getelementbyid("previewframe"); pframe.setattribute('style', "width: 100%; height: calc(100% - 88px);"); var framewidth = pframe.width; var frameheigh = pframe.height; var pimage = document.getelementbyid("previewimage"); var imagewidth = pimage.width; var imageheigh = pimage.height; alert('frame size '+framewidth+'px x '+frameheigh+'px'); } window.onload = imageinfo;
first of setattribute
method, not object property style
. should
pframe.setattribute('style', "width: 100%; height: calc(100% - 88px);");
the second problem in order read browser-calculated css properties need use getcomputedstyle
method:
var pframestyle = window.getcomputedstyle(pframe, null); var framewidth = pframestyle.width; var frameheigh = pframestyle.height;
Comments
Post a Comment