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
15 2690
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. -
var links = document.links;
-
for(var i=0;i<links.length;i++)
-
alert(links[i].getAttribute('href'));
-
Good Luck.
Kind regards,
Dmjpro.
Thank you, but as I say I'm a total beginner (hehehe) so my next question is where can I try it? XD.
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"
Try going here it's a tutorial on javascript.
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!
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
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.
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
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!!!!
gits 5,390
Expert Mod 4TB
hi ...
when clicking something you may retrieve the event target, lets assume that you click a hyperlink the:
or:
is 'A'.
kind regards
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.!!!
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
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!!!
Please use code tags when posting code:
[CODE=javascript]
JavaScript code here...
[/code]
[CODE=html]
HTML code here...
[/code]
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.
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
|
3 posts
views
Thread by Philip Herlihy |
last post: by
|
5 posts
views
Thread by Mark Rae |
last post: by
|
12 posts
views
Thread by MARTIN LANNY |
last post: by
|
2 posts
views
Thread by laredotornado |
last post: by
|
4 posts
views
Thread by Seefor |
last post: by
|
1 post
views
Thread by Robin |
last post: by
| |
4 posts
views
Thread by =?Utf-8?B?UGF1bA==?= |
last post: by
| | | | | | | | | | |