How to get urls from hyperlinks

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manuelgk
    New Member
    • Sep 2007
    • 54

    How to get urls from hyperlinks

    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
    Last edited by manuelgk; Sep 13 '07, 01:57 PM. Reason: mistakes in a word!! XD
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Originally posted by manuelgk
    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.
    [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.

    Comment

    • manuelgk
      New Member
      • Sep 2007
      • 54

      #3
      Thank you, but as I say I'm a total beginner (hehehe) so my next question is where can I try it? XD.

      Comment

      • epots9
        Recognized Expert Top Contributor
        • May 2007
        • 1352

        #4
        Originally posted by manuelgk
        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"

        Comment

        • Death Slaught
          Top Contributor
          • Aug 2007
          • 1137

          #5
          Try going here it's a tutorial on javascript.

          Comment

          • manuelgk
            New Member
            • Sep 2007
            • 54

            #6
            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

            • Death Slaught
              Top Contributor
              • Aug 2007
              • 1137

              #7
              Originally posted by manuelgk
              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

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by manuelgk
                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.

                Comment

                • gits
                  Recognized Expert Moderator Expert
                  • May 2007
                  • 5388

                  #9
                  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

                  Comment

                  • manuelgk
                    New Member
                    • Sep 2007
                    • 54

                    #10
                    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

                    • gits
                      Recognized Expert Moderator Expert
                      • May 2007
                      • 5388

                      #11
                      hi ...

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

                      [CODE=javascript]event.target.no deName
                      [/CODE]
                      or:

                      [CODE=javascript]event.target.ta gName[/CODE]
                      is 'A'.

                      kind regards

                      Comment

                      • manuelgk
                        New Member
                        • Sep 2007
                        • 54

                        #12
                        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.!!!

                        Comment

                        • gits
                          Recognized Expert Moderator Expert
                          • May 2007
                          • 5388

                          #13
                          hi ...

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

                          kind regards

                          Comment

                          • manuelgk
                            New Member
                            • Sep 2007
                            • 54

                            #14
                            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!!!
                            Last edited by acoder; Sep 27 '07, 05:37 PM. Reason: Added code tags

                            Comment

                            • acoder
                              Recognized Expert MVP
                              • Nov 2006
                              • 16032

                              #15
                              Please use code tags when posting code:
                              &#91;CODE=javas cript]
                              JavaScript code here...
                              [/CODE]

                              &#91;CODE=ht ml]
                              HTML code here...
                              [/CODE]

                              Comment

                              Working...