By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,678 Members | 1,904 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,678 IT Pros & Developers. It's quick & easy.

Javascript, Firefox and document.links

P: n/a
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

Aug 31 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a


an********@gmail.com wrote:

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


That will give you an error "document.links 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.getElementById('link_id_here')
?

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 31 '05 #2

P: n/a
Martin Honnen schrieb:


an********@gmail.com wrote:

I've discovered that the


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


is not applicable for the Firefox (even for the 1.0.6)

That will give you an error "document.links 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.getElementById('link_id_here')
?


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.getElementById('link_id_here').setAttribu te("href", "something");
Aug 31 '05 #3

P: n/a
Martin Kurz said the following on 8/31/2005 2:48 PM:
Martin Honnen schrieb:

an********@gmail.com wrote:
I've discovered that the


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


is not applicable for the Firefox (even for the 1.0.6)

That will give you an error "document.links 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.getElementById('link_id_here')
?

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.getElementById('link_id_here').setAttribu te("href", "something");


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.javascript 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?
Aug 31 '05 #4

P: n/a
>> document.getElementById('link_id_here').setAttribu te("href",
"something");

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";


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
Aug 31 '05 #5

P: n/a
Martin Kurz schrieb:
document.getElementById('link_id_here').setAttr ibute("href",
"something");

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";

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


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

object.setAttribute(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
http://msdn.microsoft.com/workshop/a...tattribute.asp

So setAttribute("href", "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
Aug 31 '05 #6

P: n/a
ASM
Martin Kurz wrote:

[attributes]
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("href", "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.


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

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.createElement('INPUT');
ntf.setAttribute('className','inpText'); // IE
ntf.setAttribute('class','inpText'); // others
ntf.id = id+ind; // don't know if ID is an attribute ?

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

var val = 'new field id = '+id+ind;
ntf.setAttribute('value',val);
var place = document.createElement('P');
place.appendChild(ntf);
document.body.appendChild(place);
}
</script>
<style type="text/css">
..inpText { background:yellow; border:2px solid red; color:blue }
</style>
<button onclick="newTextField('nouv');">New Field</button>
</html>
--
Stephane Moriaux et son [moins] vieux Mac
Aug 31 '05 #7

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


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
Sep 1 '05 #8

P: n/a
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
Yes, type on input-element seems to be readonly.
My IE (5.2 Mac)
aborts, breaks, functions when 'type' is trying to set
(see example below)
Well, seems to be a not so good errorhandling on that IE-Version. But what do
you expect in setting a readonly-Attribute? :o)
In addition :
My IE needs attribute 'className' as specified in link you gave
but ...
this attribute is not understood by FF ... !


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.getElementById(elementid).getAttribute(" className")) {
// use setAttribute("className", "")
} else if (document.getElementById(elementid).getAttribute(" class")) {
// use setAttribute("class", "")
} 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.getElementById(elementid).className = "green";

works in both browsers, IE and FF, while

document.getElementById(elementid).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
Sep 1 '05 #9

P: n/a
ASM
Martin Kurz wrote:

thanks for answer
Well, I have a question :

What is, for IE, 'type' in an input ?
is that an attribute ?
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.createElement('INPUT');
ntf.setAttribute('className','inpText'); // IE way
ntf.setAttribute('class','inpText'); // FF way
ntf.id = id+ind; // don't know if ID is an attribute ?

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

var val = 'new field id = '+id+ind;
ntf.setAttribute('value',val);
var place = document.createElement('P');
place.appendChild(ntf);
document.body.appendChild(place);
}
</script>
<style type="text/css">
..inpText { background:yellow; border:2px solid red; color:blue }
</style>
<button onclick="newTextField('nouv');">New Field</button>
</html>
It seems it is but "readonly" -> link given by M$ ->
http://www.w3.org/TR/2000/WD-DOM-Lev...tml#ID-6043025

Yes, type on input-element seems to be readonly.

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

Well, seems to be a not so good errorhandling on that IE-Version. But what do
you expect in setting a readonly-Attribute? :o)


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]
this attribute is not understood by FF ... !


And that seems to be an error in the FF-Implemantation.

....
if (document.getElementById(elementid).getAttribute(" className")) {
// use setAttribute("className", "")
No, I am still in createElement
I can't get an attribute not yet set ...
document.getElementById(elementid).className = "green";

works in both browsers, IE and FF,
Yes, this way all work fine
document.getElementById(elementid).class = "green";

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


It seems to :-/

--
Stephane Moriaux et son [moins] vieux Mac
Sep 1 '05 #10

P: n/a
> But, all my question is based on a createElement
and setting its (new) attributes on the fly
Ah, I misunderstood (because OP was only about changig given elements).
where (setting attributes to a virtual element) :
- my IE fall down, errores, breaks, if type it's tried to set
whatever I can test
By newly generated elements, you should of course being able to set the
Attribute by setAttribute("type", "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(
- 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.createElement('INPUT');
ntf.setAttribute('className','inpText'); // IE way
ntf.setAttribute('class','inpText'); // FF way
Didn't thought about the easiest way: Setting both attributes doesn't hurt the
other browsers, everyone can use the attribute he likes :)
ntf.id = id+ind; // don't know if ID is an attribute ?
Yes it is an attribute, ntf.setAttribute('id',id+ind) should bring the same
resultat.
ntf.setAttribute('type','text'); // IE breaks function here
ntf.type = 'text'; // all as with this way

var val = 'new field id = '+id+ind;
ntf.setAttribute('value',val);
var place = document.createElement('P');
place.appendChild(ntf);
document.body.appendChild(place);
}
</script>
<style type="text/css">
..inpText { background:yellow; border:2px solid red; color:blue }
CSS-Classes should start with one dot:

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

IE accepts this, FF does not.
</style>
<button onclick="newTextField('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 ... ?
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.
[className]
this attribute is not understood by FF ... !

And that seems to be an error in the FF-Implemantation.


....
if (document.getElementById(elementid).getAttribute(" className")) {
// use setAttribute("className", "")

No, I am still in createElement
I can't get an attribute not yet set ...


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.
document.getElementById(elementid).className = "green";

works in both browsers, IE and FF,

Yes, this way all work fine
document.getElementById(elementid).class = "green";

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

It seems to :-/


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("type", "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.createAttribute("type");
ntftype.nodeValue = "text";
ntf.setAttributeNode(ntftype);

Greetings

Martin
Sep 1 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.