gish Posted September 27, 2007 Share Posted September 27, 2007 hi everyone, I have been teaching myself javascript and have been doing ok but this script is not working and I am sure that it is some thing simple. I need another set of eyes just to read it. If you see the the issue I would be very happy for any information. The image is supposed to load where i want it too. <head> <title>Untitled</title> <style type="text/css"> <!-- .pics1 img { position:absolute; width:45px; height:45px; } --> </style> <style type="text/javascript"> <!-- var canvasX = 0; var canvasY = 0; function init(){ getWindowCoords(); loadimages(); } getWindowCoords = (navigator.userAgent.toLowerCase().indexOf('opera')>0||navigator.appVersion.toLowerCase().indexOf('safari')!=-1)?function() { canvasX = window.innerWidth; canvasY = window.innerHeight; }:function() { canvasX = document.documentElement.clientWidth||document.body.clientWidth||document.body.scrollWidth; canvasY = document.documentElement.clientHeight||document.body.clientHeight||document.body.scrollHeight; } function loadimages(){ var picsx = canvasX/4; var picsy = canvasy/2; pic1 = document.getElementById('button1').getElementsByTagName('img'); pic1.style.left = picsx+"px"; pic1.style.top = picsy+"px"; } window.onresize = getWindowCoords; window.onload = init; --> </style> </head> <body> <div id="button1" class="pics1"> <img src="but1.gif" alt=""> </div> </body> </html> gishaust Quote Link to comment Share on other sites More sharing options...
nogray Posted September 28, 2007 Share Posted September 28, 2007 add the array index to this line getElementsByTagName('img'); change it to getElementsByTagName('img')[0]; getElementsByTagName will always return an array. You might want to check mootools.net since they have a library that can handle the window dimenssion and related stuff. Quote Link to comment Share on other sites More sharing options...
gish Posted September 29, 2007 Author Share Posted September 29, 2007 Thankyou for the answer, and for the direction. Quote Link to comment Share on other sites More sharing options...
gish Posted September 30, 2007 Author Share Posted September 30, 2007 ok i keep getting this error 'document.getElementbyid[]'is null or not an object' I have googled it and get a lot of unnecessary information. The code that i have change is the following. pic1 = document.getElementById('button1').getElementsByTagName('img')[0]; Gishaust Quote Link to comment Share on other sites More sharing options...
nikkieijpen Posted October 2, 2007 Share Posted October 2, 2007 try this?: function loadimages(){ var picsx = canvasX/4; var picsy = canvasy/2; var pic1 = document.createElement('img'); pic1.setAttribute("src", "img1.jpg"); pic1.style.left = picsx+"px"; pic1.style.top = picsy+"px"; var div = document.getElementById('button1'); div.appendChild(pic1); } <div id="button1"></div> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.