469,579 Members | 1,899 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

How to get urls from hyperlinks

manuelgk
Hi everybody, I'm a total beginner using Javascript and I would like to know how can I get in a variable the url of hyperlinks? I want to do something like a status bar of mozilla (not a bar) but just get the urls no matter the extension of the hyperlinks (.html, .php, .pdf, etc). So please I need your help :) . Thanks
Sep 13 '07 #1
15 2632
dmjpro
2,476 2GB
Hi everybody, I just a total beginner using Javascript and I would like ti know how can I get in a variable the url of hyperlinks? I want to do something like a status bar of mozilla (not a bar) but just get the urls no matter the extension of the hyperlinks (.html, .php, .pdf, etc). So please I need your help :) . Thanks

Welcome to TSDN!
There is an attribute href of a Tag.
Try to access this.

Have an example.
Expand|Select|Wrap|Line Numbers
  1. var links = document.links;
  2. for(var i=0;i<links.length;i++)
  3.  alert(links[i].getAttribute('href'));
  4.  
Good Luck.

Kind regards,
Dmjpro.
Sep 13 '07 #2
Thank you, but as I say I'm a total beginner (hehehe) so my next question is where can I try it? XD.
Sep 13 '07 #3
epots9
1,351 Expert 1GB
Thank you, but as I say I'm a total beginner (hehehe) so my next question is where can I try it? XD.
on a HTML page with all the <a> tags with the name "links"
Sep 13 '07 #4
Death Slaught
1,137 1GB
Try going here it's a tutorial on javascript.
Sep 13 '07 #5
Thanks everybody, I follow all the advices and read the javascript tutorial, they were really useful. However, it seems like js only accept (as the first suggestion) the <a> tags with an specific URL in a Web page. My objetive is make a extension for a navigator (Mozilla), it should detect the hyperlinks no matter the web page I open. I need to take the URL's from the hyperlinks (as a status bar) and modify or send an alert before the client access the Web page. As I tell you before, the detection of the hyperlinks must be in any page of the Web. So I don't know if js is the correct option for do that or should I do it in another programming language?.
Thanks again for your help, and I'm sorry if I do stupid questions hehehe!
Sep 18 '07 #6
Death Slaught
1,137 1GB
Thanks everybody, I follow all the advices and read the javascript tutorial, they were really useful. However, it seems like js only accept (as the first suggestion) the <a> tags with an specific URL in a Web page. My objetive is make a extension for a navigator (Mozilla), it should detect the hyperlinks no matter the web page I open. I need to take the URL's from the hyperlinks (as a status bar) and modify or send an alert before the client access the Web page. As I tell you before, the detection of the hyperlinks must be in any page of the Web. So I don't know if js is the correct option for do that or should I do it in another programming language?.
Thanks again for your help, and I'm sorry if I do stupid questions hehehe!
Np it's what we're here for, and there's no such thing as a stupid question, we all had them once upon a time.

- Death
Sep 18 '07 #7
acoder
16,027 Expert Mod 8TB
Thanks everybody, I follow all the advices and read the javascript tutorial, they were really useful. However, it seems like js only accept (as the first suggestion) the <a> tags with an specific URL in a Web page. My objetive is make a extension for a navigator (Mozilla), it should detect the hyperlinks no matter the web page I open. I need to take the URL's from the hyperlinks (as a status bar) and modify or send an alert before the client access the Web page. As I tell you before, the detection of the hyperlinks must be in any page of the Web. So I don't know if js is the correct option for do that or should I do it in another programming language?.
Javascript would only work within the current page. How are you accessing the URLs? Can you give a complete example of hat you are trying to do? Also note that Javascript cannot be used for cross-domain scripting.
Sep 19 '07 #8
gits
5,390 Expert Mod 4TB
hmmmm ...

but for an extension you may retrieve the references and build up your own list ... even your own page from it ... so you may do whatever you want with that after 'making it your own' :) ... it could act like a content filter ... what i would assume that the extension should be for?

kind regards
Sep 19 '07 #9
Ok guys thanks for help me again, I was discussing with my professor about the possibility to create this, and we concluded that javascript is a language for extend the functionality of html, so the hyperlinks must have a reference (<a href></a>), thus, is impossible to be in a Web site or Web page if we don't know the direction. In other words, it happens when we want to know how a place look like and we'd never been in that place, even worse if we don't have the address, obius we never know exactly how it looks!!!, that's why we need references about where we want to go before modify something in the fuction. So my new question is how can I know the thing that I'm click is a hyperlink? do the event hadler (onmouseover, onclick) help me?
Thanks for your help again and I'm sorry if I cannot answer before... XD
Greetings from Mexico for every one!!!!
Sep 24 '07 #10
gits
5,390 Expert Mod 4TB
hi ...

when clicking something you may retrieve the event target, lets assume that you click a hyperlink the:

Expand|Select|Wrap|Line Numbers
  1. event.target.nodeName
  2.  
or:

Expand|Select|Wrap|Line Numbers
  1. event.target.tagName
is 'A'.

kind regards
Sep 24 '07 #11
Finally I resolve my problem!!!, I just needed to read the <a href> tags of the HTML document in the web navigator!! thanks everyone for helping me and I hoppe we'll see you again in this forum.!!!
Sep 25 '07 #12
gits
5,390 Expert Mod 4TB
hi ...

glad to hear you get it working ... post back to the forum anytime you have more questions ...

kind regards
Sep 25 '07 #13
Back one more time!!! XD.
Hi! again, I resolve the first problem but I have another one. My professor told me we want to catch te click event, I put the dmjpro's code and it works well with my code but with a little problem, I already catch the click event and the links too, but when I try to know or shows the URL of the clicked element, appears all the URLs of the other links if we have more than one. This is my code:

[HTML]<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">
.red_text {
color:#600;
}

.green_text {
color:#060;
}

.blue_text {
color:#006;
}
</style>

<!----The script!! XD--------->
<script type="text/javascript">
window.onload=function() {
els=document.body.getElementsByTagName('*'); <---!This is for get the "a" tags>
<!--This is for get every tag into the document, I guess XD-->
for(c=0;c<els.length;c++){
els[c].onclick=function(){
<!--Calling the fuction for clicks-->
getOnclicks(this.tagName,this.id,this.className);
}
}
}

function getOnclicks(el,id,cl) {
<!--variable for get all document's links-->
var links = document.links;

if(id==''){
id='it has no id set';
}

if(cl=''){
cl='it has no class set';
}
<!--This condition just pass everything!!, so I don't know if I can filter the "a href" tags-->

if(el){
<!--dmjpro's code>
for(var i=0;i<links.length;i++)
<!--Here is the problem, I suppose this alert gets every "href" XD-->
alert(links[i].getAttribute('href'));
}
}
</script>
</head>

<!--The HTML document to testing the script-->
<body>
<!--Here I didn't use tha class and id, this code was taking for an example in the Web XD-->
<h1 id="header" class="red_text">header</h1>
<div id="foo" class="green_text">some text</div>
<div class="blue_text">some more text</div>
<div id="image_container"><img id="my_image" src="my_image.gif" alt="my_image"/></div>
<!--links-->
<a href="http://www.uv.mx" id="hyperlink">UV</a>
<a href="http://www.google.com" id="hyperlink">Google</a>
<a href="http://www.yahoo.com.mx" id="hyperlink">Yahoo Mexico</a>
<a href="http://www.itsx.edu.mx" id="hyperlink">Tec Xalapa</a>
</body>
</html>[/HTML]

Thank's again!!!
Sep 27 '07 #14
acoder
16,027 Expert Mod 8TB
Please use code tags when posting code:
[CODE=javascript]
JavaScript code here...
[/code]

[CODE=html]
HTML code here...
[/code]
Sep 27 '07 #15
This thread is years upon years old and dead, but I stumbled upon it after a search and just had to say how impressed I was with the posts here. In most places, you wouldn't get such helpful posts but mockery considering the simple nature of the question. You guys are awesome.
Oct 27 '10 #16

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

22 posts views Thread by Justin Koivisto | last post: by
5 posts views Thread by Mark Rae | last post: by
2 posts views Thread by laredotornado | last post: by
4 posts views Thread by =?Utf-8?B?UGF1bA==?= | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.