Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onfocus operation for all links

Status
Not open for further replies.

avivit

Technical User
Jul 5, 2000
456
IL
I need to make the following for EACH link in my page:
onfocus="this.blur()"

Like this:
<a href=&quot; onfocus=&quot;this.blur()&quot;></a>

Is there a way to do it to the whole links without inserting
this line to each link? WITHOUT inserting CLASS name to each link,
without inserting a function name to each, etc.?

Something like: document.tagName.event=&quot;whatToDo()&quot;;
(I invented this one...of course..but u got the idea I guess)

I don't need this one to work on ns for a change, only IE.

Thanks in advance
 
the idea is to loop thru document.links (from the devedge doc : This array contains an entry for each Area (<AREA HREF=&quot;...&quot;> tag) and Link (<A HREF=&quot;...&quot;> tag) object in a document in source order. It also contains links created with the link method. For example, if a document contains three links, you can refer to them as: document.links[0] (...) )
then for each do
document.links[index].onfocus=blur()

or something like that. Hope for jaredn or some really nice expert to correct the details :)
 
I think that will just blur the window (or whatever the current object is when blur() is called because of the ()'s, but I'm not sure how I would do it in that context.

For IE I would use:

function linkFocusHandler()
{
var el = window.event.srcElement
if(el.tagName=='A'){el.blur();}
}

document.body.onclick=linkFocusHandler

because I ran into some difficulties with the onfocus event object not containing a sourceElement. I think iza's way of assigning the event handlers would work if you passed it a reference to my function, but the function would not have to test against tagName. The reason this works is becaus eof the event bubbling model in IE. jared@aauser.com
 
i'm still confusing the &quot;function&quot; and &quot;function()&quot; calls !
would &quot;my&quot; function work with :
document.links[index].onfocus=document.links[index].blur
???


 
it would if you made your own function that performed the blur option on the srcElement of the event. You must assign the event handler without the ()'s because when you assign a handler via script, it wants just the functions name (a function reference). When you tack () onto the end of a function (except in declaration) the intrepeter thinks it should now 'run' the function. I don't think just blur would work because the intrepeter wouldn't know what to blur, and would end up blurring the window. jared@aauser.com
 
Thanks jaredn and iza.
Well: I tried Jaredn's solution, and it works
when I replace the &quot;document.body.onclick&quot; in js,
with:
<body onclick=&quot;linkFocusHandler()&quot;>

Yet, the annoying frame around the link still
appears but for 1 sec.

So eventually I used the following:

function blurLinks(){
for(var i=0 ; i<document.links.length ; i++)
document.links.onfocus=function(){this.blur();}
}

<body onload=&quot;blurLinks()&quot;>

So it works great now.
But...I always have a following q:

I tried to change it a bit so it'll be clearer to me
but it does not work, though it looks the same:
Any ideas why?

function blurLinks()
{
for(i=0 ; i<document.links.length ; i++)
document.links.onfocus=&quot;DoIt(this)&quot;;
}
function DoIt(current)
{if(current.blur)current.blur();
}

 
here's the problem:

document.links.onfocus=&quot;DoIt(this)&quot;;

you are passing it a string and it is expecting a function reference (or inline function def)
jared@aauser.com
 
Yes, Thanks. Should be:

function blurLinks()
{
for(i=0 ; i<document.links.length ; i++)
document.links.onfocus=DoIt;
}
function DoIt()
{if(this.blur)this.blur();
}

Works perfectly now. :)
 
apparently in IE5.5 you can use:

<a href=&quot;#&quot; hidefocus>Some text</a>

or script it like so:

someobject.hideFocus=true

jared@aauser.com
 
jaredn, where do you get all of these?
This is great: When I use my way, the link is blured
and I guess that's why the active link style does not function.
But with YOUR idea the active style functions too.

I know u said it only works in IE 5.5, but I tried ns6 anyway, and u were right.
I don't have prev versions of ie, only 5.5, so
I can't check it. I hope u r wrong, and prev ie versions support it too, but you never r. R u? :)

This is cool. Really. I'm impressed.

p.s. - If anyone has an idea as for my active style problem when using my way (this.blur - mentioned in my last msg above),
I'll appreciate it.
 
I am wrong, very often :)

I checked the documentation, and it says its only supported in IE5.5

luciddream and I (when we have a short break from work) sit and stare at the 100's of properties at msdn until we find one that catches our interest. I think he found that one... jared@aauser.com
 
jaredn, can u tell me the secret?

Where exactly can I find the list of properites
for each browser?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top