Obtain HREF's

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #16
    A couple of points:

    When assigning a function to an event, don't use the parentheses, otherwise you'll be assigning the result of the function call rather than the function:
    [code=javascript]window.onload = valbeta;[/code]

    In the onclick function, use 'this' (without quotes) to refer to the current link.

    Comment

    • wangers16
      New Member
      • Jul 2007
      • 57

      #17
      I think that I have made the correct corrections, however it is producing this error 'valbeta' is undefined on line 6 (same line as the onload)


      the changes I have made are as follows:
      Code:
      function valbeta{
       var anchors = document.getElementsByTagName('a');
       for (var i = 0; i < anchors.length; ++i){
        anchors[i].onclick=function(){
         validate(this.getAttribute('href'));
         return false;
        }
       }
      }
      and this is the onload function

      Code:
      <script>
      window.onload=valbeta;
      </script>

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #18
        When you define the function, that should have parentheses:
        Code:
        function valbeta() {

        Comment

        • wangers16
          New Member
          • Jul 2007
          • 57

          #19
          done but reads out error 'valbeta' is undefined on line 6

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #20
            Hmm... interesting. I don't have any such problems with, for example, this code:
            [code=html]<html>
            <head>
            <script type="text/javascript">
            var errpage = "about:blan k"; // for testing
            /*ARRAY READER FUNCTIONS*/
            Array.prototype .contains=funct ion($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.getEle mentsByTagName( 'a');
            for (var i = 0; i < anchors.length; ++i){
            anchors[i].onclick=functi on(){
            validate(this.g etAttribute('hr ef'));
            return false;
            }
            }
            }
            function validate(url){
            outlink = url;
            if (pages.contains (outlink)==true ){
            outlink = errpage;
            }
            window.location .href=(outlink)
            }
            window.onload=v albeta;
            </script>
            </head>

            <body>
            <a href="test.html ">Test<a><b r>
            <a href="">Empty</a><br>
            <a href="error-def.htm">error</a><br>
            <a href="http://www.google.com" >google</a>
            </body>

            </html>[/code]

            Comment

            • wangers16
              New Member
              • Jul 2007
              • 57

              #21
              I have copied the code exactly and it still isn't working

              which browser are you using just out of curiousity?

              Also I ran the code through JSLint.com and corrected a couple of errors which were down to me and yet it still isn't working right and there are no errors this time

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #22
                I tested on Firefox. In IE, it doesn't work because it retrieves the full absolute href with getAttribute - see this link. The workaround for IE is to use a nonstandard flag for the second parameter.

                Comment

                • wangers16
                  New Member
                  • Jul 2007
                  • 57

                  #23
                  Ahh it is now working perfectly thanks for all of your help guys

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #24
                    No problem, glad I could help.

                    Comment

                    Working...