...

View Full Version : Getting a href from an "A" element



borger
01-31-2008, 11:50 PM
Hello,

I've a problem, I want to get the 'href' from a text by using javascript.
But I only want to see the 'href' when my cursor is on it.. so if my cursor is not between the <a></a> I don't want to get the href..
I'm getting confused of it, and I hope someone knows a solutions for it..
this is what I have so far:

my NOT working code to get the href:


document.selection.createRange().getElementsByTagName("A").href

code where I want get out of the href


<div>
bla bla bla
<a href="www.site.nl">Text1</a>
efefefef
<a href="www.blabla.nl">Text2</a>
blalbla2
</div>


so when my cursor is on 'Text1' I want to see the href of it, but when my cursor is on 'Text2' I want to see the href of that link.

Regards,


Harwin

oesxyl
02-01-2008, 12:19 AM
I suppose that this will work, I'm not sure:



document.selection.createRange().getElementsByTagName("A").innerHTML


I figure out that this can't work, becouse:
- probably must be
document.getElementsTagName('A')
- getElementsByTagName return an array
- I don't know how to use selection and createRange but I guess that must be used somewhere after getElementsByTagName
- innerHTML and href must be apply to a single element of the array

sorry, maybe somebody could help you more then I can do


best regards

borger
02-01-2008, 04:18 PM
Thanks for the fast reply, but that isn't working for my script...

it's not a problem to get the href out of this:

<a href="www.test.com">text</a>
on that moment I can use this:

document.selection.createRange().parentElement().href

but it's a problem when &#237;t is like this:
<a href="www.test.com"><strong><font>text</font></strong</a>

which code do I need to use to get the href out of it..
I hope someone else can help me

abduraooft
02-01-2008, 04:44 PM
If you can, put a unique id to that <a>

<a id="link1" href="www.test.com">text</a>
Then
document.getElementById('link1').href will work.

borger
02-01-2008, 04:56 PM
Yes that would be an easy way.. but it isn't that easy,

I don't want to use the 'id' tag.
I want to use it for a WYSIWYG script, there I want to display the 'href'.

but thanks:)

someone else haves experience with it? Or any idea's how to fix it?

borger
02-01-2008, 05:22 PM
I found a little solutions for this:


if (undefined != document.selection.createRange().parentElement().href){
var showUrl = document.selection.createRange().parentElement().href;}
else if (undefined != document.selection.createRange().parentElement().parentElement.href){
var showUrl = document.selection.createRange().parentElement().parentElement.href;}
else if (undefined != document.selection.createRange().parentElement().parentElement.parentElement.href){
var showUrl = document.selection.createRange().parentElement().parentElement.parentElement.href;}

thanks for your reply's!
if someone knows a better way to do this, let me know;) and if not, than this topic can be closed.

Regards,


Harwin

Rohan_Shenoy
02-01-2008, 06:08 PM
Thats a post on my blog. easy!
http://rohanshenoy.blogspot.com/2008/01/print-links-in-printable-version-using.html



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum