Connecting Tech Pros Worldwide Help | Site Map

getElementsByTagName + getAttribute("class")??

hutch@satellitecreative.com
Guest
 
Posts: n/a
#1: Oct 20 '05
Hi,

I'm making use of this VERY useful function for a so called 'standards
compliant' replacement for target="_blank":

function externalLinks() {
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++) {
var anchor = anchors[i];
if (anchor.getAttribute("href") && anchor.getAttribute("rel") ==
"extlink") anchor.target = "_blank";
}
} window.onload = externalLinks;

and using it with:

<a href="mylink" rel="extlink" class="extlink">link text</a>

What I'd really like to do is make it even better and do away with the
'rel', and have it that each anchor with the "extlink" class applied to
it gets processed by the function.

I have tried replacing the relevant code with this line:

if (anchor.getAttribute("href") && anchor.getAttribute("class") ==
"extlink") anchor.target = "_blank";

cut i get 'null' as the response [i alert
'anchor.getAttribute("class")']
So any ideas why this doesnt work?

Eventually I'd like to have it that all I need to do is apply the
'extlink' class to a link, and it'll be styled as I want it AND open in
a new window.

Any pointers appreciated,
Ben

Michael Winter
Guest
 
Posts: n/a
#2: Oct 20 '05

re: getElementsByTagName + getAttribute("class")??


On 20/10/2005 10:32, hutch@satellitecreative.com wrote:
[color=blue]
> I'm making use of this VERY useful function for a so called 'standards
> compliant' replacement for target="_blank":[/color]

[code adding target attribute through script]

If you add a target attribute via a script to a document with a Strict
DTD, the resulting document tree is just as invalid as it would be if
the target attribute was present in the markup. Either use a
Transitional DTD, where the target attribute is defined, or use a Strict
DTD and ignore the error.

[snip]
[color=blue]
> if (anchor.getAttribute("href") && anchor.getAttribute("class") ==
> "extlink") anchor.target = "_blank";
>
> cut i get 'null' as the response [i alert
> 'anchor.getAttribute("class")'][/color]

With IE, I assume?

IE's implementation of the *Attribute methods is broken. In this
instance, it expects 'className' not 'class'.

Don't use the *Attribute methods; use the property shortcuts, like
anchor.className, instead.

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Gomolyako Eduard
Guest
 
Posts: n/a
#3: Oct 20 '05

re: getElementsByTagName + getAttribute("class")??


See to Microsoft CSS Behaviors.

You can define external link in the following way:
HTML:
<head>
<style type="text/css">
a.extLink {
/*Some css properties here*/
behavior: url(extlink.htc);
}
</style>
</head>
<body>
<a href="somesite.com" class="extLink">Open somesite.com in new
window</a>
</body>

extlink.htc file:
<public:component>
<public:attach event="oncontentready" onevent="init()" />

<script language="javascript">

function init()
{
if (element && element.tagName && element.tagName.toLowerCase() ==
"a")
element.target = "_blank";
}

</script>
</public:component>

Best, Ed.

hutch@satellitecreative.com писал(а):
[color=blue]
> Hi,
>
> I'm making use of this VERY useful function for a so called 'standards
> compliant' replacement for target="_blank":
>
> function externalLinks() {
> if (!document.getElementsByTagName) return;
> var anchors = document.getElementsByTagName("a");
> for (var i=0; i<anchors.length; i++) {
> var anchor = anchors[i];
> if (anchor.getAttribute("href") && anchor.getAttribute("rel") ==
> "extlink") anchor.target = "_blank";
> }
> } window.onload = externalLinks;
>
> and using it with:
>
> <a href="mylink" rel="extlink" class="extlink">link text</a>
>
> What I'd really like to do is make it even better and do away with the
> 'rel', and have it that each anchor with the "extlink" class applied to
> it gets processed by the function.
>
> I have tried replacing the relevant code with this line:
>
> if (anchor.getAttribute("href") && anchor.getAttribute("class") ==
> "extlink") anchor.target = "_blank";
>
> cut i get 'null' as the response [i alert
> 'anchor.getAttribute("class")']
> So any ideas why this doesnt work?
>
> Eventually I'd like to have it that all I need to do is apply the
> 'extlink' class to a link, and it'll be styled as I want it AND open in
> a new window.
>
> Any pointers appreciated,
> Ben[/color]

Hutch
Guest
 
Posts: n/a
#4: Oct 26 '05

re: getElementsByTagName + getAttribute("class")??


> IE's implementation of the *Attribute methods is broken. In this[color=blue]
> instance, it expects 'className' not 'class'.[/color]

Thanks, thats sorted it.

Ben
Closed Thread