maguskrool
Technical User
Hi there.
I have an image I need to resize whenever the browser windowis resized. After reading other posts and general javascript functioning, I wrote this code:
If I use a value like "300px" instead of document.body.offsetWidth in imgNewSize, it works as expected. So what am I doing wrong?
Thanks in advance.
I have an image I need to resize whenever the browser windowis resized. After reading other posts and general javascript functioning, I wrote this code:
Code:
/*from my_code.js*/
function imgNewSize() {
document.getElementById("my_image").style.width = document.body.offsetWidth;
}
/*from my_site.html*/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
<script language="JavaScript" src="javascript/my_code.js" type="text/javascript">
</script>
</head>
<body onresize="imgNewSize();">
<img id="my_image" name="my_image" src="images/1.jpg" />
</body>
</html>
If I use a value like "300px" instead of document.body.offsetWidth in imgNewSize, it works as expected. So what am I doing wrong?
Thanks in advance.