Obtain HREF's

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wangers16
    New Member
    • Jul 2007
    • 57

    Obtain HREF's

    Hi all,

    Does anyone know how to get JavaScript to retrieve the hyperlinks that are stored on a page and then use them later on?

    Thanks in advance
  • rohypnol
    New Member
    • Dec 2007
    • 54

    #2
    Originally posted by wangers16
    Does anyone know how to get JavaScript to retrieve the hyperlinks that are stored on a page and then use them later on?
    var anchors = document.getEle mentsByTagName( 'a');
    for (var i = 0; i < anchors.length; ++i)
    {
    alert(anchors[i].getAttribute(' href'));
    }

    Comment

    • wangers16
      New Member
      • Jul 2007
      • 57

      #3
      and how could I use this to display or manipulate the link when that link has been clicked?

      e.g. if the user has clicked upon a link which was stored in an array then he/she would be redirected and if not then they would go to the specified page

      http://www.thescripts.com/forum/thread700874.html, this is what I am currently using atm which requires me to add the following to all href's:

      javascript:vali date('someurl')

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Add the validate function using onclick rather than as a href, i.e. when you loop over the links, add an onclick which passes this.href to the validate function.

        Comment

        • wangers16
          New Member
          • Jul 2007
          • 57

          #5
          yes, but I would like it to be able to validate simply from this <a href="someurl"> text</a> alone.

          Is this possible and if so how?

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            No, you will need to use some JavaScript code to detect clicks and deal with them.

            You can make it unobtrusive - separate the JavaScript from the HTML - if that's what you want.

            Comment

            • wangers16
              New Member
              • Jul 2007
              • 57

              #7
              ok

              how can this be achieved?

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                What rohypnol posted, but set the onclick of each anchor in the loop and the function should be called after the page has loaded:
                [code=javascript]anchor.onclick = function() {
                // call validate() here
                }[/code]

                Comment

                • wangers16
                  New Member
                  • Jul 2007
                  • 57

                  #9
                  I have tried what you said with the following:

                  Code:
                  var anchors = document.getElementsByTagName('a');
                  for (var i = 0; i < anchors.length; ++i)
                  {
                  anchors.onclick=function(){
                  validate(anchors[i].getAttribute('href'));
                  }
                  }
                  function validate(url){
                  outlink = url
                  if (pages.contains(outlink)==true){
                  outlink = errpage;
                  }
                  window.location.href=(outlink)
                  }
                  however it doesn't seem to work, have I coded it correctly?

                  I have also included the function that checks the url as well in case it is needed

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    That should be anchors[i].onclick...

                    Comment

                    • wangers16
                      New Member
                      • Jul 2007
                      • 57

                      #11
                      still doesn't work...

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Oh right, I did mention earlier (but perhaps didn't make it clear) that this should be run after page load.

                        Put the code in a function and call it on page load, e.g.
                        [code=javascript]window.onload = functionName;[/code]

                        Comment

                        • wangers16
                          New Member
                          • Jul 2007
                          • 57

                          #13
                          done and it returns a Not Implemented error on the same line as the onload function and still doesn't work

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            Can you post the latest version of the code?

                            Comment

                            • wangers16
                              New Member
                              • Jul 2007
                              • 57

                              #15
                              here is the full code

                              Code:
                              /*ARRAY READER FUNCTIONS*/
                              Array.prototype.contains=function($str){
                              var $blFound=false;
                              var $reg = new RegExp("^" + $str + "$", "i")
                              for (var i = 0;i<this.length;i++){
                              	 if(this[i].match($reg)){
                              	 $blFound=true;
                              	}
                              }
                              return $blFound;
                              }
                              /*END OF ARRAY READER FUNCTIONS*/
                              /*BLOCKED PAGES*/
                              var pages = ['','error-def.htm'];
                              /*END OF BLOCKED PAGES*/
                              /*LINK VALIDATION FUNCTIONS*/
                              function valbeta(){
                              var anchors = document.getElementsByTagName('a');
                              for (var i = 0; i < anchors.length; ++i){
                              anchors[i].onclick=function(){
                              validate(anchors[i].getAttributeNode('href'));
                              return false;
                              }
                              }
                              }
                              function validate(url){
                              outlink = url;
                              if (pages.contains(outlink)==true){
                              outlink = errpage;
                              }
                              window.location.href=(outlink)
                              }
                              /*END OF LINK VALIDATION FUNCTIONS*/
                              and this is the script on the page used to call the function onload:

                              Code:
                              <script>
                              document.onload=valbeta();
                              </script>
                              p.s. window.onload doesn't work either I decided to try and use the document.onload to see if that would work but it didn't

                              Comment

                              Working...