Jump to content

load picture


gish

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/70959-load-picture/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/70959-load-picture/#findComment-357606
Share on other sites

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/70959-load-picture/#findComment-358292
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/70959-load-picture/#findComment-359961
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.