Javascript, Firefox and document.links

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andreister@gmail.com

    Javascript, Firefox and document.links

    He there!
    I've discovered that the

    =============== =============== =============== ====
    document.links( 'link_id_here') .href = "something" ;
    =============== =============== =============== ====

    is not applicable for the Firefox (even for the 1.0.6), and I'm falling
    into passing through the array and checking each link id.

    Could anybody confirm it's true and/or direct to a more elegant
    solution if any?


    Thanx,
    Andrew

  • Martin Honnen

    #2
    Re: Javascript, Firefox and document.links



    andreister@gmai l.com wrote:

    [color=blue]
    > I've discovered that the[/color]
    [color=blue]
    > document.links( 'link_id_here') .href = "something" ;[/color]
    [color=blue]
    > is not applicable for the Firefox (even for the 1.0.6)[/color]

    That will give you an error "document.l inks is not a function" indeed.
    Why do you think that document.links is a function?
    And if you really have an id why don't you simply use
    document.getEle mentById('link_ id_here')
    ?

    --

    Martin Honnen

    Comment

    • Martin Kurz

      #3
      Re: Javascript, Firefox and document.links

      Martin Honnen schrieb:[color=blue]
      >
      >
      > andreister@gmai l.com wrote:
      >
      >[color=green]
      >> I've discovered that the[/color]
      >
      >[color=green]
      >> document.links( 'link_id_here') .href = "something" ;[/color]
      >
      >[color=green]
      >> is not applicable for the Firefox (even for the 1.0.6)[/color]
      >
      >
      > That will give you an error "document.l inks is not a function" indeed.
      > Why do you think that document.links is a function?
      > And if you really have an id why don't you simply use
      > document.getEle mentById('link_ id_here')
      > ?
      >[/color]

      Correct, document.links is an array, you can get a single element by ie
      document.links[0] for the first link in document, the links-count is in
      document.links. length and the href isn't called hraf but target:
      document.links[0].target.

      And the functionality you're looking for has been posted by Martin Honnen, then
      you can set the href by

      document.getEle mentById('link_ id_here').setAt tribute("href", "something" );

      Comment

      • Randy Webb

        #4
        Re: Javascript, Firefox and document.links

        Martin Kurz said the following on 8/31/2005 2:48 PM:
        [color=blue]
        > Martin Honnen schrieb:
        >[color=green]
        >>
        >>andreister@gm ail.com wrote:
        >>
        >>
        >>[color=darkred]
        >>>I've discovered that the[/color]
        >>
        >>[color=darkred]
        >>>document.lin ks('link_id_her e').href = "something" ;[/color]
        >>
        >>[color=darkred]
        >>>is not applicable for the Firefox (even for the 1.0.6)[/color]
        >>
        >>
        >>That will give you an error "document.l inks is not a function" indeed.
        >>Why do you think that document.links is a function?
        >>And if you really have an id why don't you simply use
        >> document.getEle mentById('link_ id_here')
        >>?
        >>[/color]
        >
        >
        > Correct, document.links is an array, you can get a single element by ie
        > document.links[0] for the first link in document, the links-count is in
        > document.links. length and the href isn't called hraf but target:
        > document.links[0].target.
        >
        > And the functionality you're looking for has been posted by Martin Honnen, then
        > you can set the href by
        >
        > document.getEle mentById('link_ id_here').setAt tribute("href", "something" );[/color]

        And wonder why it doesn't work properly in the majority of browsers
        since IE has major problems with setAttribute? Set it dierectly:

        document.links['linkNameNotID'].href="new HREF for the link";

        --
        Randy
        comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
        Answer:It destroys the order of the conversation
        Question: Why?
        Answer: Top-Posting.
        Question: Whats the most annoying thing on Usenet?

        Comment

        • Martin Kurz

          #5
          Re: Javascript, Firefox and document.links

          >> document.getEle mentById('link_ id_here').setAt tribute("href",[color=blue][color=green]
          >> "something" );[/color]
          >
          >
          > And wonder why it doesn't work properly in the majority of browsers
          > since IE has major problems with setAttribute? Set it dierectly:
          >
          > document.links['linkNameNotID'].href="new HREF for the link";[/color]

          Really? Didn't noticed yet (but all Clients I'm scripting for are on Intranet
          and so they're all IE6 (if IE at all) and there I didn't got any problems yet)

          But document.links['linkNameNotID'] isn't correct, links is (as posted earlier
          an arry, not a hash) - some browser may support this but at least IE 6 does not
          (document.links ... is null or not an object)

          Martin

          Comment

          • Martin Kurz

            #6
            Re: Javascript, Firefox and document.links

            Martin Kurz schrieb:[color=blue][color=green][color=darkred]
            >>>document.get ElementById('li nk_id_here').se tAttribute("hre f",
            >>>"something") ;[/color]
            >>
            >>
            >>And wonder why it doesn't work properly in the majority of browsers
            >>since IE has major problems with setAttribute? Set it dierectly:
            >>
            >>document.link s['linkNameNotID'].href="new HREF for the link";[/color]
            >
            >
            > Really? Didn't noticed yet (but all Clients I'm scripting for are on Intranet
            > and so they're all IE6 (if IE at all) and there I didn't got any problems yet)
            >
            > But document.links['linkNameNotID'] isn't correct, links is (as posted earlier
            > an arry, not a hash) - some browser may support this but at least IE 6 does not
            > (document.links ... is null or not an object)
            >
            > Martin[/color]

            Well, setAttribute on IE seems to be a little bit intolerant about speeling of
            the attributes (in special about the case):

            object.setAttri bute(sName, vValue [, iFlags])

            If the specified attribute is not already present, the setAttribute method
            adds the attribute to the object and sets the value.

            Be careful when spelling attribute names. If you set iFlags to 1 and the
            sName parameter does not have the same uppercase and lowercase letters as the
            attribute, a new attribute is created for the object.

            If two or more attributes have the same name, differing only in case, and
            iFlags is set to 0, this method assigns values only to the first attribute
            created with this name. All other attributes of the same name are ignored.

            When setting the CLASS attribute using this method, set the sName to be
            "className" , which is the corresponding Dynamic HTML (DHTML) property.

            See
            Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


            So setAttribute("h ref", "URL") shouldn't bring up any problems in IE (as every
            other attribute too when regarding the spelling and case) and is working in
            Firefox like requested.

            Martin

            Comment

            • ASM

              #7
              Re: [attributes] Javascript, Firefox and document.links

              Martin Kurz wrote:

              [attributes]
              [color=blue]
              > When setting the CLASS attribute using this method, set the sName to be
              > "className" , which is the corresponding Dynamic HTML (DHTML) property.
              >
              > See
              > http://msdn.microsoft.com/workshop/a...tattribute.asp
              >
              > So setAttribute("h ref", "URL") shouldn't bring up any problems in IE (as every
              > other attribute too when regarding the spelling and case) and is working in
              > Firefox like requested.[/color]

              Well, I have a question :

              What is, for IE, 'type' in an input ?
              is that an attribute ?
              It seems it is but "readonly" -> link given by M$ ->


              My IE (5.2 Mac)
              aborts, breaks, functions when 'type' is trying to set
              (see example below)

              In addition :
              My IE needs attribute 'className' as specified in link you gave
              but ...
              this attribute is not understood by FF ... !


              example :

              <html>
              <script type="text/javascript">
              var ind = 0;
              function newTextField(id ) {
              ind++;
              var ntf = document.create Element('INPUT' );
              ntf.setAttribut e('className',' inpText'); // IE
              ntf.setAttribut e('class','inpT ext'); // others
              ntf.id = id+ind; // don't know if ID is an attribute ?

              ntf.setAttribut e('type','text' ); // IE breaks function here
              ntf.type = 'text'; // all as with this way

              var val = 'new field id = '+id+ind;
              ntf.setAttribut e('value',val);
              var place = document.create Element('P');
              place.appendChi ld(ntf);
              document.body.a ppendChild(plac e);
              }
              </script>
              <style type="text/css">
              ..inpText { background:yell ow; border:2px solid red; color:blue }
              </style>
              <button onclick="newTex tField('nouv'); ">New Field</button>
              </html>


              --
              Stephane Moriaux et son [moins] vieux Mac

              Comment

              • RobG

                #8
                Re: Javascript, Firefox and document.links

                Martin Kurz wrote:
                [...]
                [color=blue]
                > But document.links['linkNameNotID'] isn't correct, links is (as posted earlier
                > an arry, not a hash)[/color]

                It's not an array, it's an HTML collection. It is a bit like an array -
                it has a length property and you can access the items by index or
                name/id - but it does not have array methods like sort, join, etc.

                <URL:http://www.w3.org/TR/2000/PR-DOM-Level-2-HTML-20000927/html.html#ID-75708506>


                --
                Rob

                Comment

                • Martin Kurz

                  #9
                  Re: [attributes] Javascript, Firefox and document.links

                  [color=blue]
                  > Well, I have a question :
                  >
                  > What is, for IE, 'type' in an input ?
                  > is that an attribute ?
                  > It seems it is but "readonly" -> link given by M$ ->
                  > http://www.w3.org/TR/2000/WD-DOM-Lev...tml#ID-6043025[/color]

                  Yes, type on input-element seems to be readonly.
                  [color=blue]
                  > My IE (5.2 Mac)
                  > aborts, breaks, functions when 'type' is trying to set
                  > (see example below)[/color]

                  Well, seems to be a not so good errorhandling on that IE-Version. But what do
                  you expect in setting a readonly-Attribute? :o)
                  [color=blue]
                  > In addition :
                  > My IE needs attribute 'className' as specified in link you gave
                  > but ...
                  > this attribute is not understood by FF ... ![/color]

                  And that seems to be an error in the FF-Implemantation. className isn't just
                  specified in the link I gave, it's specified in the
                  /WD-DOM-Level-1-20000929-Link you gave too. Easiest way seems to me to test an
                  element of which you know it has a class-attrib in the html-source. So you could
                  check the correct class-Attribute-name:

                  if (document.getEl ementById(eleme ntid).getAttrib ute("className" )) {
                  // use setAttribute("c lassName", "")
                  } else if (document.getEl ementById(eleme ntid).getAttrib ute("class")) {
                  // use setAttribute("c lass", "")
                  } else {
                  // something else
                  }

                  Not the easiest way for setting class-attribs - but the correct one.

                  What's really interesting on the ff-behavior: Setting the Class the following way

                  document.getEle mentById(elemen tid).className = "green";

                  works in both browsers, IE and FF, while

                  document.getEle mentById(elemen tid).class = "green";

                  works in none, so FF seems to be a little bit inconsequent.

                  I prefer this method because of it's the "oficial" way and working - so why
                  should I use another method that's working on some actual browsers but maybe
                  won't work in the next generation. If using it this way, if it isn't working in
                  any browser, at least I can say, that it's the browsers fault, not mine, and
                  trying to get around the bug in these special cases ;o)

                  greetings,

                  Martin

                  Comment

                  • ASM

                    #10
                    Re: [attributes] Javascript, Firefox and document.links

                    Martin Kurz wrote:

                    thanks for answer
                    [color=blue][color=green]
                    >>Well, I have a question :
                    >>
                    >>What is, for IE, 'type' in an input ?
                    >>is that an attribute ?[/color][/color]

                    But, all my question is based on a createElement
                    and setting its (new) attributes on the fly

                    where (setting attributes to a virtual element) :
                    - my IE fall down, errores, breaks, if type it's tried to set
                    whatever I can test
                    - my IE wants 'className' as attribute for new class
                    while my FF wants 'class' for same attribute

                    All was in my comemted example I give here back :

                    example :

                    <html>
                    <script type="text/javascript">
                    var ind = 0;
                    function newTextField(id ) {
                    ind++;
                    var ntf = document.create Element('INPUT' );
                    ntf.setAttribut e('className',' inpText'); // IE way
                    ntf.setAttribut e('class','inpT ext'); // FF way
                    ntf.id = id+ind; // don't know if ID is an attribute ?

                    ntf.setAttribut e('type','text' ); // IE breaks function here
                    ntf.type = 'text'; // all as with this way

                    var val = 'new field id = '+id+ind;
                    ntf.setAttribut e('value',val);
                    var place = document.create Element('P');
                    place.appendChi ld(ntf);
                    document.body.a ppendChild(plac e);
                    }
                    </script>
                    <style type="text/css">
                    ..inpText { background:yell ow; border:2px solid red; color:blue }
                    </style>
                    <button onclick="newTex tField('nouv'); ">New Field</button>
                    </html>


                    [color=blue][color=green]
                    >>It seems it is but "readonly" -> link given by M$ ->
                    >>http://www.w3.org/TR/2000/WD-DOM-Lev...tml#ID-6043025[/color]
                    >
                    >
                    > Yes, type on input-element seems to be readonly.
                    >
                    >[color=green]
                    >>My IE (5.2 Mac)
                    >>aborts, breaks, functions when 'type' is trying to set
                    >>(see example below)[/color]
                    >
                    >
                    > Well, seems to be a not so good errorhandling on that IE-Version. But what do
                    > you expect in setting a readonly-Attribute? :o)[/color]

                    I try to tell to that new input element (createElement)
                    it's attribute would be one of types :
                    - text
                    - checkbox
                    - radio
                    - button
                    - submit
                    - reset
                    If I (and you, and they, we) can't set this type,
                    how to do to get some more radio-buttons, or check-boxes, or ... ?

                    [className]
                    [color=blue][color=green]
                    >>this attribute is not understood by FF ... ![/color]
                    >
                    > And that seems to be an error in the FF-Implemantation.[/color]
                    ....
                    [color=blue]
                    > if (document.getEl ementById(eleme ntid).getAttrib ute("className" )) {
                    > // use setAttribute("c lassName", "")[/color]

                    No, I am still in createElement
                    I can't get an attribute not yet set ...
                    [color=blue]
                    > document.getEle mentById(elemen tid).className = "green";
                    >
                    > works in both browsers, IE and FF,[/color]

                    Yes, this way all work fine
                    [color=blue]
                    > document.getEle mentById(elemen tid).class = "green";
                    >
                    > works in none, so FF seems to be a little bit inconsequent.[/color]

                    It seems to :-/



                    --
                    Stephane Moriaux et son [moins] vieux Mac

                    Comment

                    • Martin Kurz

                      #11
                      Re: [attributes] Javascript, Firefox and document.links

                      > But, all my question is based on a createElement[color=blue]
                      > and setting its (new) attributes on the fly[/color]

                      Ah, I misunderstood (because OP was only about changig given elements).
                      [color=blue]
                      > where (setting attributes to a virtual element) :
                      > - my IE fall down, errores, breaks, if type it's tried to set
                      > whatever I can test[/color]

                      By newly generated elements, you should of course being able to set the
                      Attribute by setAttribute("t ype", "inputtype" ) - but changing the attribute this
                      way is without effect. So the type can be given once and is then only readable.
                      So that's really a bug in your IE (and the IEs Mac-Version is famous for its
                      bugs, especially in JS :o(
                      [color=blue]
                      > - my IE wants 'className' as attribute for new class
                      > while my FF wants 'class' for same attribute
                      >
                      > All was in my comemted example I give here back :
                      >
                      > example :
                      >
                      > <html>
                      > <script type="text/javascript">
                      > var ind = 0;
                      > function newTextField(id ) {
                      > ind++;
                      > var ntf = document.create Element('INPUT' );
                      > ntf.setAttribut e('className',' inpText'); // IE way
                      > ntf.setAttribut e('class','inpT ext'); // FF way[/color]

                      Didn't thought about the easiest way: Setting both attributes doesn't hurt the
                      other browsers, everyone can use the attribute he likes :)
                      [color=blue]
                      > ntf.id = id+ind; // don't know if ID is an attribute ?[/color]

                      Yes it is an attribute, ntf.setAttribut e('id',id+ind) should bring the same
                      resultat.
                      [color=blue]
                      > ntf.setAttribut e('type','text' ); // IE breaks function here
                      > ntf.type = 'text'; // all as with this way
                      >
                      > var val = 'new field id = '+id+ind;
                      > ntf.setAttribut e('value',val);
                      > var place = document.create Element('P');
                      > place.appendChi ld(ntf);
                      > document.body.a ppendChild(plac e);
                      > }
                      > </script>
                      > <style type="text/css">
                      > ..inpText { background:yell ow; border:2px solid red; color:blue }[/color]

                      CSS-Classes should start with one dot:

                      ..inpText { background:yell ow; border:2px solid red; color:blue }

                      IE accepts this, FF does not.
                      [color=blue]
                      > </style>
                      > <button onclick="newTex tField('nouv'); ">New Field</button>
                      > </html>
                      >
                      > I try to tell to that new input element (createElement)
                      > it's attribute would be one of types :
                      > - text
                      > - checkbox
                      > - radio
                      > - button
                      > - submit
                      > - reset
                      > If I (and you, and they, we) can't set this type,
                      > how to do to get some more radio-buttons, or check-boxes, or ... ?[/color]

                      OK, as said before, I misunderstood the problem. On creating the element, you're
                      able to set the type-attribute but it's not changeable.
                      [color=blue]
                      > [className]
                      >[color=green][color=darkred]
                      >>> this attribute is not understood by FF ... ![/color]
                      >>
                      >>
                      >> And that seems to be an error in the FF-Implemantation.[/color]
                      >
                      > ....
                      >[color=green]
                      >> if (document.getEl ementById(eleme ntid).getAttrib ute("className" )) {
                      >> // use setAttribute("c lassName", "")[/color]
                      >
                      >
                      > No, I am still in createElement
                      > I can't get an attribute not yet set ...[/color]

                      Well, the same here: Other problem, so i described the wrong way. But simply
                      setting both, class AND className, as you did, seems to work fine.
                      [color=blue][color=green]
                      >> document.getEle mentById(elemen tid).className = "green";
                      >>
                      >> works in both browsers, IE and FF,[/color]
                      >
                      >
                      > Yes, this way all work fine
                      >[color=green]
                      >> document.getEle mentById(elemen tid).class = "green";
                      >>
                      >> works in none, so FF seems to be a little bit inconsequent.[/color]
                      >
                      >
                      > It seems to :-/[/color]

                      So IE5/mac has a bug in setting the Type-Attribute by setAttribute, so you have
                      to get around this bug by using another way, ie ntf.type = "".

                      On Windows, the posted code with setAttribute("t ype", "text") works fine in IE6
                      and after correcting the .. in the css in FF too.

                      I've still one last alternative, a little more code, but when generating new
                      elements the "cleaner" version: Does createElement work in IE/Mac?

                      var ntftype = document.create Attribute("type ");
                      ntftype.nodeVal ue = "text";
                      ntf.setAttribut eNode(ntftype);

                      Greetings

                      Martin

                      Comment

                      Working...