Create array on load page

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Francois

    #1

    Create array on load page

    Is it possible to automatically create an array on load page?

    I want my script to be external.
    (<script language="JavaS cript" src="somescript .js"></script>)

    Several pages should use this script. I have an unknown number of <DIV>
    elements in each page, all with a unique id (ID="name"). I need an array
    with all the names of these div-elements in it, the order is unimportant.

    Can someone help me out here?


  • Laurent Bugnion, GalaSoft

    #2
    Re: Create array on load page

    Hi,

    Francois wrote:[color=blue]
    > Is it possible to automatically create an array on load page?
    >
    > I want my script to be external.
    > (<script language="JavaS cript" src="somescript .js"></script>)
    >
    > Several pages should use this script. I have an unknown number of <DIV>
    > elements in each page, all with a unique id (ID="name"). I need an array
    > with all the names of these div-elements in it, the order is unimportant.
    >
    > Can someone help me out here?[/color]

    You can create an array when the page loads, however this array will be
    only loaded in RAM, moreover only in the scope of the window where the
    script is executed. If you want that other windows have access to this
    array, you must leave the document in the window, untouched, so that
    other windows can access it. If you load another document in the window,
    the array will be deleted.

    You cannot save the array in a file using client-side JavaScript. You
    could save it in a cookie, providing you are sure that the client
    supports cookie, and that the serialized array will fit in the cookie
    (size...).

    Does it sound like something responding to your needs?

    Laurent
    --
    Laurent Bugnion, GalaSoft
    Webdesign, Java, JavaScript: http://www.galasoft-LB.ch
    Private/Malaysia: http://mypage.bluewin.ch/lbugnion
    Support children in Calcutta: http://www.calcutta-espoir.ch

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Create array on load page

      "Francois" <francois.sachs @gidesign,nl> writes:
      [color=blue]
      > Is it possible to automatically create an array on load page?[/color]

      If you want something to happen when a page has finished loading,
      you should put it in the body's onload handler.

      <body onload="doSomet hing()">
      [color=blue]
      > I want my script to be external.
      > (<script language="JavaS cript" src="somescript .js"></script>)[/color]

      Put the code below in "somescript.js" .
      [color=blue]
      > Several pages should use this script. I have an unknown number of <DIV>
      > elements in each page, all with a unique id (ID="name"). I need an array
      > with all the names of these div-elements in it, the order is unimportant.[/color]

      ---
      var divNames=[];
      function doSomething() {
      var divs;
      if (document.getEl ementsByTagName ) {
      divs = document.getEle mentsByTagName( "div"); // Modern browsers
      } else {
      divs = document.all.ta gs["div"]; // Old IE browsers
      }
      for (var i=0;i<divs.leng th;i++) {
      divNames[i]=divs[i].id;
      }
      }
      ---
      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Richard Cornford

        #4
        Re: Create array on load page

        "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
        news:vftcxiiw.f sf@hotpop.com.. .
        <snip>[color=blue]
        >divs = document.all.ta gs["div"]; // Old IE browsers[/color]
        <snip>

        Genuine IE browsers seem very tolerant of the use of - [ ... ] - instead
        of - ( ... ) - and vice versa, but - tags - is a method of the - all -
        object so a more formally correct syntax would be:-

        divs = document.all.ta gs("div"); // Old IE browsers,
        // and unusual IE spoofers.

        Richard.


        Comment

        Working...