SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jun 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    document.images.length DOESN'T WORK

    HI, I have got sth like this:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html xmlns=”http://www.w3.org/1999/xhtml”>
    <head>
    <title>Szablon JavaScript</title>
    <meta http-equiv="content-type" content="text/html;charset=ISO-8859-2" />
    <link rel="stylesheet" href="style.css" type="text/css" />
    <script type="text/javascript" src="skrypt.js" charset="ISO-8859-2"></script>
    </head>
    <body>
    <img name="obrazek1" src="rys1.gif" />
    </body>
    </html>
    and script:
    document.write(document.images.length);
    The result of this script is 0, but there is a one image. Anyone knows why it's 0?
    And the second example, which doesn't work:
    :
    <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
    “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
    <html xmlns=”http://www.w3.org/1999/xhtml”>
    <head>
    <title>Chapter 6: Example 3</title>
    </head>
    <body>
    <img name=”img1” src=”” border=”0” width=”200” height=”150” />
    <script type=”text/javaScript”>
    var myImages = new Array(“usa.gif”,”canada.gif”,”jamaica.gif”,”mexico.gif”);
    var imgIndex = prompt(“Enter a number from 0 to 3”,””);
    document.images[“img1”].src = myImages[imgIndex];
    </script>
    </body>
    </html>
    This script should change image if user put different number, but it does NOTHING and I dont know why

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by dudek2 View Post
    The result of this script is 0, but there is a one image. Anyone knows why it's 0?
    At the ime that the script runs, the rest of the page doesn't exist yet, which is why you're getting 0.

    Quote Originally Posted by dudek2 View Post
    And the second example, which doesn't work:
    The main problem that immediately stands out there is that fancy quotes are used, which are not accepted by HTML. You need to use double quotes there instead.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Jun 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you're absolutely right!!! Now everything is clear!!! Thank U so much!!

  4. #4
    SitePoint Member
    Join Date
    Jun 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you know why this code doesn't work? If images are in <ul></ul> doesn't work, but if images are outside <ul></ul> it is OK:

    <ul>
    <li><img src="fot1.jpg" id="fot1"/></li>
    <li><img src="fot2.jpg"/></li>
    <li><img src="fot3.jpg"/></li>
    </ul>
    <input type="button" onclick="enlarge()" value="Zmien rozmiar" />

    and script:

    function enlarge(){
    document.images[0].width *= 1.2;
    }

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    It seems to work for me whether they are in or out of the list area.

    Perhaps there's something else about the page that's causing interference.
    Can you please link us to a test page that demonstrates the problem?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •