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
How to get urls from hyperlinks
Collapse
X
-
Originally posted by manuelgkHi 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.
[code=javascript]
var links = document.links;
for(var i=0;i<links.len gth;i++)
alert(links[i].getAttribute(' href'));
[/code]
Good Luck.
Kind regards,
Dmjpro. -
-
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!Comment
-
Originally posted by manuelgkThanks 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!
- DeathComment
-
Originally posted by manuelgkThanks 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?.Comment
-
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 regardsComment
-
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!!!!Comment
-
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=f unction() {
els=document.bo dy.getElementsB yTagName('*'); <---!This is for get the "a" tags>
<!--This is for get every tag into the document, I guess XD-->
for(c=0;c<els.l ength;c++){
els[c].onclick=functi on(){
<!--Calling the fuction for clicks-->
getOnclicks(thi s.tagName,this. id,this.classNa me);
}
}
}
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.len gth;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_te xt">some text</div>
<div class="blue_tex t">some more text</div>
<div id="image_conta iner"><img id="my_image" src="my_image.g if" 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.m x" id="hyperlink"> Yahoo Mexico</a>
<a href="http://www.itsx.edu.mx " id="hyperlink"> Tec Xalapa</a>
</body>
</html>[/HTML]
Thank's again!!!Comment
Comment