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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.