473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

getElementsByTagName + getAttribute("class")??

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

Oct 20 '05 #1
3 21891
On 20/10/2005 10:32, hu***@satellitecreative.com wrote:
I'm making use of this VERY useful function for a so called 'standards
compliant' replacement for target="_blank":
[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]
if (anchor.getAttribute("href") && anchor.getAttribute("class") ==
"extlink") anchor.target = "_blank";

cut i get 'null' as the response [i alert
'anchor.getAttribute("class")']


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.
Oct 20 '05 #2
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.

hu***@satellitecreative.com писал(а):
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


Oct 20 '05 #3
> IE's implementation of the *Attribute methods is broken. In this
instance, it expects 'className' not 'class'.


Thanks, thats sorted it.

Ben
Oct 26 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Arvid Andersson | last post by:
Hello, I need to convert a string to a number, but the string can contain +,-,* and / as well as parenthesis. For example, if I have the string "30/(6+9)" I would like a function that returned the...
12
by: Romain | last post by:
Hi, I've used an excellent way to make target="_blank" links that still validate while using HTML 4.01 Transitional doctypes in the past. I learned the trick from :...
2
by: CES | last post by:
All, I'm at a loss, the code below works in IE but not in Netscape and I'm unskilled enough not to know why. Essentially this code looks thru all of the form fields and if the input box has a...
3
by: F. Da Costa | last post by:
Hi, I was wondering *why* there is a difference between the results of the following two statements. On the suface they seem to do the same (or do they?) frm => returns void ...
8
by: Calan | last post by:
I have a server-side ASP script that dynamically creates an input form from a database table. The table contains a field name, the table where values are stored, type of input control, value for a...
3
by: davidkarlsson74 | last post by:
Error: document.getElementById("folderMenu").cells has no properties File: http://www.volkswagen.se/tillbehor/js/foldermenu.js Rad: 49 The function activates different DIV:s, but doesn't seem to...
11
by: jesdynf | last post by:
I'm having trouble applying a stylesheet to content I'm generating after the fact. Here's the sample code: <html> <head> <title>CSS/DOM Problem Example</title> <style type="text/css">...
15
by: judge82 | last post by:
I made sure I closed all the bracket, why do i still get the error. on line 360, 424, 425 and 607 Please help /* * ShopCartServletHW3.java *
7
by: SM | last post by:
Hello, i need to add a classname to an element <ausing JavaScript. I've tried a couple of thing with no success. This is an small piece of my code: .... <li><a href="<?php echo $cd_img; ?>"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.