473,573 Members | 2,909 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getElementsByTa gName + getAttribute("c lass")??

Hi,

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

function externalLinks() {
if (!document.getE lementsByTagNam e) return;
var anchors = document.getEle mentsByTagName( "a");
for (var i=0; i<anchors.lengt h; i++) {
var anchor = anchors[i];
if (anchor.getAttr ibute("href") && anchor.getAttri bute("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.getAttr ibute("href") && anchor.getAttri bute("class") ==
"extlink") anchor.target = "_blank";

cut i get 'null' as the response [i alert
'anchor.getAttr ibute("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 21909
On 20/10/2005 10:32, hu***@satellite creative.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.getAttr ibute("href") && anchor.getAttri bute("class") ==
"extlink") anchor.target = "_blank";

cut i get 'null' as the response [i alert
'anchor.getAttr ibute("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.classNam e, 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:compone nt>
<public:attac h event="onconten tready" onevent="init() " />

<script language="javas cript">

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

</script>
</public:componen t>

Best, Ed.

hu***@satellite creative.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.getE lementsByTagNam e) return;
var anchors = document.getEle mentsByTagName( "a");
for (var i=0; i<anchors.lengt h; i++) {
var anchor = anchors[i];
if (anchor.getAttr ibute("href") && anchor.getAttri bute("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.getAttr ibute("href") && anchor.getAttri bute("class") ==
"extlink") anchor.target = "_blank";

cut i get 'null' as the response [i alert
'anchor.getAttr ibute("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
2328
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 number 2. I actually wrote a java function that did this a couple of years ago, in school, as an excersise in "binary trees". I lost it, and most...
12
3274
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 : http://www.sitepoint.com/article/1041 Basically, instead of typing : <a href="yourlink.html" target="_blank" alt="">The link</a>
2
5454
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 attribute of disabled="true", attribute would only be present if the field was disabled, then the label field's class is changed to a grayed out...
3
1815
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 frm.getAttribute("custom") => returns the value of the attribute
8
2021
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 label, etc. What I need to do is create a JS validation routine that will check each control for valid input, regardless of what the control name...
3
9253
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 work on FireFox or Netscape. What could be wrong? The function: function setActiveTab(tabNo) {
11
2423
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"> ..historylinks {
15
3329
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
2241
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; ?>" onClick="show_picture(this); return false;">image 1</a></li> <li><a href="<?php echo $cd_img; ?>" onClick="show_picture(this); return false;">image...
0
7781
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
8027
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8202
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7788
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8066
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5594
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5292
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3733
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1304
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.