conditional url

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • geraldjr30
    New Member
    • Jan 2009
    • 60

    conditional url

    hi,

    i would like to know how i can evaluate the current url so that either javascript:hist ory.go(-1)'or javascript:hist ory.go(-2) is executed. I would like for the javascript:hist ory.go(-2) to be executed if the url is suffixed with the "#top"... like http://localhost...#top. I currently have:

    Code:
    echo "<A HREF='javascript:javascript:history.go(-1)'>HOME</A><br><br>";

    thanks in advance,
    geebee
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    test for the window.location .hash property. (MDC – window.location)

    Comment

    • geraldjr30
      New Member
      • Jan 2009
      • 60

      #3
      now i have:

      Code:
      <html>
      
      <BODY>
      
      <SCRIPT LANGUAGE="JavaScript">
      
       function showLoc()
       {
         var x = window.location;
      
      if (x = "http://httplocalhost/checkurl.html")
      {
      document.write("<a href='http://www.microsoft.com'>go to microsoft</a>");
      }
      else
      {
      document.write "stay here"
      }
      
         var t = ['Property - Typeof - Value',
                  'window.location - ' + (typeof x) + ' - ' + x ];
         for (var prop in x){
           t.push(prop + ' - ' + (typeof x[prop]) + ' - ' +  (x[prop] || 'n/a'));
         }
         alert(t.join('\n'));
       }
      </SCRIPT>
      
      
      
      </html>
      but its not doing anything...



      saying "error on page" at the bottom of the window.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        firstly, window.location is an object, not a string. secondly, there's a syntax error in line 18 (missing brackets) and probably the wrong operator on line 12. thirdly, javascript should be served with a type declaration in the <script> tag.

        Comment

        • geraldjr30
          New Member
          • Jan 2009
          • 60

          #5
          ok..

          i figure it out.. here is my code... appearing at the bottom after the PHP code:
          Code:
          <SCRIPT LANGUAGE="JavaScript">
           function showLoc()
           {
             var x = window.location;
          if (x == "http://localhost/checkurl.html")
          {
          document.write("<a href='http://www.microsoft.com'>go to microsoft</a>");
          }
          else
          {
          document.write ("stay here")
          }
          
           }
          
          x=0;
          document.self.location.reload();
          </SCRIPT>

          the only problem now is i cant figure out how to get the document.write results after the results of my PHP are listed. i tried <BODY ONLOAD = ...> but that only lists the document.write output from the javascript.

          thanks in advance,
          geebee

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            Originally posted by geraldjr30
            the only problem now is i cant figure out how to get the document.write results after the results of my PHP are listed.
            what PHP code?

            just out of interest, do you ever enter the first statement in the if clause?

            Comment

            • geraldjr30
              New Member
              • Jan 2009
              • 60

              #7
              yes, i entered the first line of the IF statement. but it only works if i have the <BODY ONLOAD = showLoc()..> and then my PHP output does not display. only the document.write output from the javascript function displays. i want the PHP results to display as a result of my PHP code (not posted here because it is irrelevant i think), and then the document.write from the javascript function to be displayed beneath that.

              thanks in advance,
              geebee

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                Originally posted by geraldjr30
                yes, i entered the first line of the IF statement. but it only works if i have the <BODY ONLOAD = showLoc()..>
                obviously.... the output is defined in a function after all

                Originally posted by geraldjr30
                and then my PHP output does not display.
                then there is something wrong with your PHP code.

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Originally posted by geraldjr30
                  only the document.write output from the javascript function displays.
                  document.write reopens the document for writing, so don't use it after the page has loaded. Either use DOM methods, set innerHTML of an element or use it during page load only.

                  Comment

                  • geraldjr30
                    New Member
                    • Jan 2009
                    • 60

                    #10
                    ok... i think im almost there..

                    i have..
                    Code:
                    <?php>
                    ...
                    ...
                    ?>
                    
                    <SCRIPT LANGUAGE="JavaScript">
                    function showLoc()
                     {
                       var x = window.location;
                    if (x == "http://localhost/REC.php?count=sunday&fac=des&SRG_GROUP=Charity")
                    {
                    
                    document.body.innerHTML += "<a href=\"http://www.AOL.com\">AOL</a>";
                    }
                    else
                    {
                    document.body.innerHTML += "<a href=\"http://www.msn.com\">MICROSOFT</a>";
                    }
                    }
                    </SCRIPT>
                    but neither AHREF is being shown on the page...

                    thanks in advance,
                    geebee

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Are you calling showLoc body onload?

                      Comment

                      • geraldjr30
                        New Member
                        • Jan 2009
                        • 60

                        #12
                        now i am calling it on BODY load.. but the MICROSOFT link is appearing. The url in the address bar is



                        so it should be listing the AOL link instead... am i evaluating the current url wrong? once again my code is...

                        Code:
                        <SCRIPT LANGUAGE="JavaScript">
                        function showLoc()
                         {
                           var x = window.location;
                        if (x == "http://localhost/REC.php?count=sunday&fac=DES&SRG_GROUP=Charity")
                        {
                        
                        document.body.innerHTML += "<a href=\"http://www.AOL.com\">AOL</a>";
                        }
                        else
                        {
                        document.body.innerHTML += "<a href=\"http://www.msn.com\">MICROSOFT</a>";
                        }
                        }
                        </SCRIPT>
                        thanks in advance,
                        geebee

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #13
                          try this one:
                          Code:
                          if (x.href == "http://localhost/REC.php?count=sunday&fac=DES&SRG_GROUP=Charity")

                          Comment

                          • geraldjr30
                            New Member
                            • Jan 2009
                            • 60

                            #14
                            ok thanks... that works!!!! now all i need to figure out is to evaluate whether or not the url in the address bar is suffixed with "#top", as there may be any day followed by the word "count" in the url, and i would rather not have an if statement for ever day of the week. how would i amend this?

                            thanks in advance,
                            geebee

                            Comment

                            • Dormilich
                              Recognized Expert Expert
                              • Aug 2008
                              • 8694

                              #15
                              Originally posted by geraldjr30
                              ok thanks... that works!!!!
                              I've told you about that problem already in post #4 (and #6)........
                              Originally posted by geraldjr30
                              now all i need to figure out is to evaluate whether or not the url in the address bar is suffixed with "#top"
                              use window.location .hash (see post #2)

                              regards

                              Comment

                              Working...