?
Solved

please see what is wrong with my JS code

Posted on 2012-03-22
8
Medium Priority
?
219 Views
Last Modified: 2012-03-23
Hi,

Practicing a little JS..

Please see what is wrong with my JS code below:
It is meant to move the image across the screen.

<html>
<head><title>Our Universe</title>
<script type="text/javascript">

var i=0;
var timeout;
var elementStyle = document.images("image1").style;
elementStyle.position = "relative";
elementStyle.left = "5px";

function driveCar() {

      while (i<100) {
            elementStyle.left = i+"px";
            i++;
            timeout = setTimeout("driveCar()",1500);
      
}
}


</script>
<body onload="driveCar();">

<img src="images.jpg" name="image1" id="image1" />


</body>
</html>
0
Comment
Question by:jecommera
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 38

Accepted Solution

by:
Tom Beck earned 1600 total points
ID: 37754901
For one thing, you will not be able to access the image element from the head section until it has been rendered to the page. You could move your script block to somewhere below the image tag on the page.
0
 
LVL 35

Assisted Solution

by:Robert Schutt
Robert Schutt earned 400 total points
ID: 37754946
how about this:

<script type="text/javascript">

var i=0;
var timeout;
var elementStyle = null;

function driveCar() {

      if (!elementStyle)
      {
            elementStyle = document.images("image1").style;
            elementStyle.position = "relative";
            elementStyle.left = "5px";
      }

      if (i<1000) {
            elementStyle.left = i+"px";
            i++;
            timeout = setTimeout("driveCar();",100);
      }
}

</script>

Open in new window

0
 

Author Comment

by:jecommera
ID: 37754957
Thanks however I get error

document.images is not a function
elementStyle = document.images("image1").style;
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 38

Assisted Solution

by:Tom Beck
Tom Beck earned 1600 total points
ID: 37754959
Well, if we are doing complete re-writes then how about this:
<body onload="driveCar_init();">

<img src="images.jpg" name="image1" id="image1" />

<script type="text/javascript">
var i=0;
var timeout;
var elementStyle = document.getElementById("image1").style;
elementStyle.position = "relative";
elementStyle.left = "5px";

function driveCar() {
    elementStyle.left = i+"px";
    i++; 
}
function driveCar_init(){
    timeout = setInterval("driveCar()",500);
}
</script>
</body>

Open in new window

0
 
LVL 38

Assisted Solution

by:Tom Beck
Tom Beck earned 1600 total points
ID: 37754974
document.images is a collection of all images on the page. Since you only have one, you could do this: document.images[0].style. Generally though it is more precise to select the element by id: document.getElementById("image1").style
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 37754983
wow, @tommyBoy, getting a bad vibe here, I was just trying to show that you can use a script in the head section.

To get back to you @jecommera: In my browser here it works with parentheses but maybe you need to use square brackets for 'normal' browsers:

elementStyle = document.images["image1"].style;

Open in new window

0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 37755002
Must be my fatherly instinct. When I see someone is trying to learn, I try to teach. Do it for them and they never learn. I have three very smart grown up children.
0
 

Author Closing Comment

by:jecommera
ID: 37757308
yes I am practicing JavaScript so any of these I raise is to learn more so than get a solution that works.<br /><br />Got load of explanations above which help<br /><br />thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question