Web Analytics Made Easy -
StatCounter Passing div and layer names into a function, how?? - CodingForum

Announcement

Collapse
No announcement yet.

Passing div and layer names into a function, how??

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Passing div and layer names into a function, how??

    I have a function that writes to a div, layer or ilayer depending upon the browser. I need the same function to write to a number of divs etc all with different names (id). I pass the div etc names to the function as a strings, how do I get that string so that I can use it as the actual name.

    The code they will be used in is: (myStr is the bit that will be written)

    document.ilayername.document.layername.document.write(myStr);

    and

    divname.innerHTML=myStr;

    or any other way I can pass the div etc names as parameters.

    Thanks
    An answer needs a question just as much as a question needs an answer. Deep eh!

  • #2
    document['ilayername'].document['layername'].document.write()

    And

    document.all['divnameorid']

    And finally the DOM way:

    document.getElementById('divid')

    You just need to pass a single string argument this way.
    jasonkarldavis.com

    Comment


    • #3
      Code:
      function getLayer(id) {
      return typeof document.all != 'undefined' ? document.all(id) :
      typeof document.getElementById != 'undefined' ? document.getElementById(id) :
      typeof document.layers != 'undefined' ? document['NS' + id].document.layers[0] : null;
      }
      
      function write_to_layer(id,.....) {
      var l = getLayer(id);
      if (l) {
      .....etc.
      I'm guessing you're nesting the layers in NS4 for positioning purposes (table?); if so, just id the container element (<td>, <div>, <span>) and give the enclosed <ilayer> the same id with 'NS' prepended to it. I've always wanted to use 'prepended' in a forum.

      You 'insert' pieces of object references in JS by using associative array (rather than dot) notation; so...

      document.form_name.element_name

      ...is more easily modified as

      document[form_name][element_name]

      ...by simply replacing the string 'key' in the brackets.

      Comment


      • #4
        I like to think of the brackets as property accessors. This goes for more than just properties though:

        15.0['toString']().constructor == String //true

        For example.
        jasonkarldavis.com

        Comment


        • #5
          Thanks guys, stunning. It now works in IE6, NS6, NS4.7 and Mozilla, the only one that throwing it's toys out is Opera, but having said that HV Menu, which I use for navigation, does not work in Opera either, sso no big loss.
          Thanks again.
          An answer needs a question just as much as a question needs an answer. Deep eh!

          Comment

          Working...
          X