SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member hawkeye46's Avatar
    Join Date
    Apr 2003
    Location
    Naperville, Illinois
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript, Netscape, image swapping

    I have written a simple little script that works flawlessly in IE. However, when I use Netscape the image swapping on page load does not work. Here is an example:
    function up2date(){
    here(2); lunch(3);
    }
    function here(id){
    window.document.images( "emp" + id + "a" ).src = "pics/in.gif";
    window.document.images( "emp" + id + "b" ).src = "pics/spacer.gif";
    window.document.images( "emp" + id + "c" ).src = "pics/spacer.gif";
    window.document.images( "emp" + id + "d" ).src = "pics/spacer.gif";
    }
    function lunch(id){
    window.document.images( "emp" + id + "a" ).src = "pics/spacer.gif";
    window.document.images( "emp" + id + "b" ).src = "pics/lunch.gif";
    window.document.images( "emp" + id + "c" ).src = "pics/spacer.gif";
    window.document.images( "emp" + id + "d" ).src = "pics/spacer.gif";
    }

    I am querying our mysql database for the status of our employees and write out javascript like: here(2), for anyone that is 'here()' or 'lunch()' . That would then move the image when the <body onLoad=up2date()> occurs. Can anyone please point me in the right direction with this annoying little problem.

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    function here(id) {
    document.images["emp" + id + "a"].src = "pics/in.gif";
    document.images["emp" + id + "b"].src = "pics/spacer.gif";
    document.images["emp" + id + "c"].src = "pics/spacer.gif";
    document.images["emp" + id + "d"].src = "pics/spacer.gif";
    }

    MS browsers let you dereference arrays using either parentheses (a la VBScript) or square brackets. Use the ECMA-compliant approach.
    ::: certified wild guess :::

  3. #3
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by adios
    MS browsers let you dereference arrays using either parentheses (a la VBScript) or square brackets. Use the ECMA-compliant approach.
    Well, I never knew that! Not that it's actually useful, because of course you shouldn't do it...! lol
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by adios
    MS browsers let you dereference arrays using either parentheses (a la VBScript) or square brackets. Use the ECMA-compliant approach.
    That's mostly accurate. It's actually not arrays that you are dereferencing, but objects or collections (which are NodeLists). Since even arrays in JS are objects, then you can dereference their properties (such as length), but not their indexes (which makes sense, since JS doesn't support associative arrays and there's no need to dereference a numerically indexed array)

    Collections are most like associative arrays, in that both a named key and numerical index point at the same value. However, be not fooled, collections are not associative arrays. They can't be sorted and are read-only (immutable).
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Posted 7-01-03 to:

    Brendan Eich, mozilla.org
    ....inventor of JavaScript
    "JS doesn't support associative arrays..."
    Posted here. I beg to differ. Am I incorrect?
    Reply 7-01-03:
    He's mixing up JS arrays, which like all JS objects are associative arrays of properties (a property is named by a string in JS -- when you use o[42] or a similar integer "array element index", you are just using a shorthand for o['42]'), and DOM collections, which are restricted. He also seems to claim that associative arrays must be sortable and mutable, but I know of no such restriction in the common definition.

    See http://www.jennifermadden.com/162/ex...raySimple.html, first hit for "associative array definition" in google.

    /be
    [posted w/o comment]
    ::: certified wild guess :::

  6. #6
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not convinced. First of all, the page he linked to mentions only associatve array notation, and even then only in reference to the built-in collections, which I admitted were much like associative arrays, but not really (even he admitted they were restricted). I concede that the capacity to be mutated or sorted is not helpful in defining an associative array.

    An associative array's indexes can be strings OR numbers (the two are mutually exclusive). This is why collections don't qualify. PHP supports an associative array that is not present in javascript, for example.

    The ambiguity here, I believe, is that everything in javascript is an object. Yes, I suppose you can stretch the definition of an associative array to objects (particulary javascript's Array objects), since named properties are associated with their value. However, there is a problem with this definitional stretching. If I have an array with four numeric indicies and three named (custom) properties (which certainly falls within the definition of an associative array) the length of the array is still only four.

    It is only because javascript treats the arrays as objects that we can add named values - otherwise we'd be stuck with just numeric indicies. Remember, even an associative array should still be an array, meaning a sequence of values. Named properties on an array object aren't part of that sequence. Yes, a for...in loop will catch both custom properties and named indexes, but why should I have to resort to that to dissect something that should be a vector? Something that should have a defined and meaningful length? (besides the undesireable effect of picking up any prototyped properties, as well)
    Quote Originally Posted by Brenden
    a property is named by a string in JS -- when you use o[42] or a similar integer "array element index", you are just using a shorthand for o['42]'
    I'm really confused by this. Even forgiving his typo, it doesn't make much sense. Yes, o[42] and o['42'] both point to the same index. I assume the script engine translates the string version of "42" into a usable integer. That doesn't mean that o['42'] is a property. This just seems to further cement the fact that JS doesn't support associative arrays. In a true associative array, o[42] and o['42'] would be two different key/value pairs.

    If I concede to his definition that "all JS objects are associative arrays of properties" then why should I call them associative arrays when the lexicon already has a term for this data structure? (Hint: "OBJECT" ) And as I already mentioned, objects cannot have numerical property names, which is a trait associative arrays must have to be both associative and arrays. And, since I feel I've sufficiently proven that javascript arrays maintain only numeric indexes as part of their vector, then we will just call them just plain arrays.

    These differences can lead me to no other conclusion that javascript doesn't support what C considers to be an associative array. JSObjects may fall under the associative array definition for another language, but since javascript is (supposedly) a C-based scripting language, I see no point in entertaining other such definitions.

    Someone, please talk some sense into me, because if JS really does support true associative arrays, it's interface to them stinks.
    Last edited by beetle; Jul 2, 2003 at 14:16.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  7. #7
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ---------------------
    KO!
    ---------------------
    Round 2 . . . FIGHT!!
    ---------------------

    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ....tried to get God to answer this...but He was busy with more pressing issues. Stupidly assumed the INVENTOR OF JAVASCRIPT - never mind his other (lengthy) credentials - would suffice. Apparently he doesn't understand how his language works. Film at 11.

    The word 'chutzpah' comes to mind here.

    The 'forgiveness' about the typo was classic...why even mention it unless you're arguing like a lawyer with a guilty client?
    ::: certified wild guess :::

  9. #9
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Chutzpah indeed [img]images/smilies/wink.gif[/img]

    I agree that javascript implements objects as associative arrays, adios. My point is, just because JS implements objects as associative arrays, doesn't mean I can create one. The scripter (you and I) aren't given an interface to an associative-array-like data structure. Heck, I could care less if JS implemented everything as linked-lists

    If I'm wrong, then please show me how to make an array with both named and numbered keys, where all key/value pairs are part of the array's vector, that is, where I can iterate over the array's length and expose all key/value pairs.

    That's all I ask.

    Until then, I stand by my opinion that "object" is a more approriate label for this data structure.

    I'm not saying I'm right, I would just like proof that I'm wrong. I haven't really seen that yet.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  10. #10
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ive just installed ns and found my image swap funtion still works
    Code:
    function h(im,ih){document.images[im].src = (ih).src}
    and swap the image using
    Code:
    ......onmouseover=h(this,"NewImage")
    ......onmouseover=h("ImageName","NewImage")
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if


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
  •