How do I always chop off both query string and "#" from document.location.href?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Powell

    How do I always chop off both query string and "#" from document.location.href?

    Code:
    if (document.location.href.indexOf('?') >= 0) document.location.href
    = document.location.href.substring(0,
    document.location.href.indexOf('?'));
    
    if (document.location.href.indexOf('#') >= 0) {
    document.location.href = document.location.href.substring(0,
    document.location.href.indexOf('#')) + '?' + newUrl;
    } else {
    document.location.href = document.location.href + '?' + newUrl;
    }
    I thought this would simply and always remove the query string and the
    "#" from a URL, however, it does not. Using alert() proves the query
    string and the "#" is still embedded within the URL, furthermore, it
    tacks on the query string on top of itself time and time again each
    time this snippet is done upon a user click.

    All I want to do is do "document.locat ion.href" and chop off the query
    string and "#" before it redirects in Javascript.

    Thanx
    Phil
  • McKirahan

    #2
    Re: How do I always chop off both query string and "#&quot ; from document.locati on.href?

    "Phil Powell" <phillip.s.powe ll@gmail.com> wrote in message
    news:b5476f2f.0 410181330.21c82 f39@posting.goo gle.com...[color=blue]
    >
    Code:
    > if (document.location.href.indexOf('?') >= 0) document.location.href
    > = document.location.href.substring(0,
    > document.location.href.indexOf('?'));
    >
    > if (document.location.href.indexOf('#') >= 0) {
    > document.location.href = document.location.href.substring(0,
    > document.location.href.indexOf('#')) + '?' + newUrl;
    > } else {
    > document.location.href = document.location.href + '?' + newUrl;
    > }
    >
    >
    >
    > I thought this would simply and always remove the query string and the
    > "#" from a URL, however, it does not. Using alert() proves the query
    > string and the "#" is still embedded within the URL, furthermore, it
    > tacks on the query string on top of itself time and time again each
    > time this snippet is done upon a user click.
    >
    > All I want to do is do "document.locat ion.href" and chop off the query
    > string and "#" before it redirects in Javascript.
    >
    > Thanx
    > Phil[/color]

    Will this work for you?

    var href = document.locati on.href;
    href = href.substring( 0,href.indexOf( "?"));
    href = href.substring( 0,href.indexOf( "#"));
    alert(href);


    Comment

    • J. J. Cale

      #3
      Re: How do I always chop off both query string and &quot;#&quot ; from document.locati on.href?


      "Phil Powell" <phillip.s.powe ll@gmail.com> wrote in message
      news:b5476f2f.0 410181330.21c82 f39@posting.goo gle.com...[color=blue]
      >
      Code:
      > if (document.location.href.indexOf('?') >= 0) document.location.href
      > = document.location.href.substring(0,
      > document.location.href.indexOf('?'));[/color]
      
      you may need to assign document.location.href to a variable
      var str = document.location.href;
      alert (document.location.href.substring(0,
      document.location.href.indexOf('?')));
      in IE5 this returns 'file///mypath/href.htm'
      check this out. You can name it anything you want. I used href.htm for the
      test.
      
      <HTML><HEAD><TITLE></TITLE></HEAD>
      <BODY>
      <br><br><br>
      <script type="text/javascript">
      alert('document.location.href = '+document.location.href);
      var queryString = 'duh', url = 'href.htm?duh';
      // using your code to assign value to str
      var str = document.location.href.substring(0,
      document.location.href.indexOf('?'));
      // check to see if you've pressed the button and sent a query string
      if(document.location.search) {
      //  if so toggle the query string
      document.location.search.indexOf('duh') != -1?queryString =
      'wow':queryString = 'duh';
      alert ('Your code produces '+str+'\n\nClick again to see it change');
      url = str+'?'+queryString;
      }
      </script>
      <button onclick = "document.location.href=url">Click to get new href with
      query</button>
      </BODY></HTML>
      [color=blue]
      > if (document.location.href.indexOf('#') >= 0) {
      > document.location.href = document.location.href.substring(0,
      > document.location.href.indexOf('#')) + '?' + newUrl;
      > } else {
      > document.location.href = document.location.href + '?' + newUrl;
      > }
      >
      >
      >
      > I thought this would simply and always remove the query string and the
      > "#" from a URL, however, it does not. Using alert() proves the query
      > string and the "#" is still embedded within the URL, furthermore, it
      > tacks on the query string on top of itself time and time again each
      > time this snippet is done upon a user click.
      >
      > All I want to do is do "document.locat ion.href" and chop off the query
      > string and "#" before it redirects in Javascript.
      >
      > Thanx
      > Phil[/color]

      Hope it helps.
      Jimbo


      Comment

      • Philip Ronan

        #4
        Re: How do I always chop off both query string and &quot;#&quot ; fromdocument.lo cation.href?

        Am I missing something here?

        What's wrong with "location.pathn ame"?

        --
        Philip Ronan
        phil.ronanzzz@v irgin.net
        (Please remove the "z"s if replying by email)


        Comment

        • McKirahan

          #5
          Re: How do I always chop off both query string and &quot;#&quot ; fromdocument.lo cation.href?

          "Philip Ronan" <phil.ronanzzz@ virgin.net> wrote in message
          news:BD9AE7DD.2 406E%phil.ronan zzz@virgin.net. ..[color=blue]
          > Am I missing something here?
          >
          > What's wrong with "location.pathn ame"?
          >
          > --
          > Philip Ronan
          > phil.ronanzzz@v irgin.net
          > (Please remove the "z"s if replying by email)
          >[/color]


          Don't you mean:

          location.protoc ol + "//" + location.host + location.pathna me


          Comment

          • Philip Ronan

            #6
            Re: How do I always chop off both query string and &quot;#&quot;fr omdocument.loca tion.href?

            McKirahan wrote:
            [color=blue]
            > "Philip Ronan" <phil.ronanzzz@ virgin.net> wrote in message
            > news:BD9AE7DD.2 406E%phil.ronan zzz@virgin.net. ..[color=green]
            >>
            >> What's wrong with "location.pathn ame"?[/color]
            >
            > Don't you mean:
            >
            > location.protoc ol + "//" + location.host + location.pathna me[/color]

            Uhh, yeah. If you want an absolute URL.

            What's the purpose of all this "document.locat ion.href.indexO f('?')" stuff?

            --
            Philip Ronan
            phil.ronanzzz@v irgin.net
            (Please remove the "z"s if replying by email)


            Comment

            • McKirahan

              #7
              Re: How do I always chop off both query string and &quot;#&quot;fr omdocument.loca tion.href?

              "Philip Ronan" <phil.ronanzzz@ virgin.net> wrote in message
              news:BD9AF2C8.2 4083%phil.ronan zzz@virgin.net. ..[color=blue]
              > McKirahan wrote:
              >[color=green]
              > > "Philip Ronan" <phil.ronanzzz@ virgin.net> wrote in message
              > > news:BD9AE7DD.2 406E%phil.ronan zzz@virgin.net. ..[color=darkred]
              > >>
              > >> What's wrong with "location.pathn ame"?[/color]
              > >
              > > Don't you mean:
              > >
              > > location.protoc ol + "//" + location.host + location.pathna me[/color]
              >
              > Uhh, yeah. If you want an absolute URL.
              >
              > What's the purpose of all this "document.locat ion.href.indexO f('?')"[/color]
              stuff?[color=blue]
              >
              > --
              > Philip Ronan
              > phil.ronanzzz@v irgin.net
              > (Please remove the "z"s if replying by email)
              >[/color]

              I screwed up. It should have been:

              var href = document.locati on.href;
              if (href.indexOf(" ?") > 0) href = href.substring( 0,href.indexOf( "?"));
              if (href.indexOf(" #") > 0) href = href.substring( 0,href.indexOf( "#"));
              alert(href);

              Just a variation of what the OP that works.


              Comment

              • Phil Powell

                #8
                Re: How do I always chop off both query string and &quot;#&quot ; from document.locati on.href?

                Philip Ronan <phil.ronanzzz@ virgin.net> wrote in message news:<BD9AE7DD. 2406E%phil.rona nzzz@virgin.net >...[color=blue]
                > Am I missing something here?
                >
                > What's wrong with "location.pathn ame"?[/color]

                That didn't work either. I gave up and did a server-side solution
                instead a bit more elaborate but functions. This was something I
                don't think I could ever tackle.

                Phil

                Comment

                • Phil Powell

                  #9
                  Re: How do I always chop off both query string and &quot;#&quot ; from document.locati on.href?

                  "J. J. Cale" <photom@netvisi on.net.il> wrote in message news:<41752392$ 1@news.012.net. il>...[color=blue]
                  > "Phil Powell" <phillip.s.powe ll@gmail.com> wrote in message
                  > news:b5476f2f.0 410181330.21c82 f39@posting.goo gle.com...[color=green]
                  > >
                  Code:
                  > > if (document.location.href.indexOf('?') >= 0) document.location.href
                  > > = document.location.href.substring(0,
                  > > document.location.href.indexOf('?'));[/color]
                  >
                  > you may need to assign document.location.href to a variable
                  > var str = document.location.href;
                  > alert (document.location.href.substring(0,
                  > document.location.href.indexOf('?')));
                  > in IE5 this returns 'file///mypath/href.htm'
                  > check this out. You can name it anything you want. I used href.htm for the
                  > test.[/color]
                  
                  I tried the same thing, to no avail.  In Firefox it still bombed out
                  and never redirected because, for some reason, assigning
                  document.[anything at all] to a variable completely knocks out
                  redirection and prevents it from ever happening whatsoever.  Assigning
                  to a variable fails.
                  
                  This is what I have so far and it will never work with a query string;
                  without one it's just fine and puts one on there.  If a query string
                  is found it redirects to the wrong URL (a URL w/o a query string) but
                  then goes back to the RIGHT URL if you click again.
                  
                  [CODE]
                  if (document.location.href.indexOf('?') >= 0) document.location.href
                  = document.location.href.substring(0,
                  document.location.href.indexOf('?'));
                  
                  if (document.location.href.indexOf('#') >= 0) {
                  document.location.href = document.location.href.substring(0,
                  document.location.href.indexOf('#')) + '?' + newUrl;
                  } else {
                  document.location.href += '?' + newUrl + additionalQS;
                  }
                  Phil
                  [color=blue]
                  >
                  > <HTML><HEAD><TI TLE></TITLE></HEAD>
                  > <BODY>
                  > <br><br><br>
                  > <script type="text/javascript">
                  > alert('document .location.href = '+document.loca tion.href);
                  > var queryString = 'duh', url = 'href.htm?duh';
                  > // using your code to assign value to str
                  > var str = document.locati on.href.substri ng(0,
                  > document.locati on.href.indexOf ('?'));
                  > // check to see if you've pressed the button and sent a query string
                  > if(document.loc ation.search) {
                  > // if so toggle the query string
                  > document.locati on.search.index Of('duh') != -1?queryString =
                  > 'wow':queryStri ng = 'duh';
                  > alert ('Your code produces '+str+'\n\nClic k again to see it change');
                  > url = str+'?'+querySt ring;
                  > }
                  > </script>
                  > <button onclick = "document.locat ion.href=url">C lick to get new href with
                  > query</button>
                  > </BODY></HTML>
                  >[color=green]
                  > > if (document.locat ion.href.indexO f('#') >= 0) {
                  > > document.locati on.href = document.locati on.href.substri ng(0,
                  > > document.locati on.href.indexOf ('#')) + '?' + newUrl;
                  > > } else {[/color]
                  > document.locati on.href = document.locati on.href + '?' + newUrl;[color=green]
                  > > }
                  > >
                  > > [/CODE]
                  > >
                  > > I thought this would simply and always remove the query string and the
                  > > "#" from a URL, however, it does not. Using alert() proves the query
                  > > string and the "#" is still embedded within the URL, furthermore, it
                  > > tacks on the query string on top of itself time and time again each
                  > > time this snippet is done upon a user click.
                  > >
                  > > All I want to do is do "document.locat ion.href" and chop off the query
                  > > string and "#" before it redirects in Javascript.
                  > >
                  > > Thanx
                  > > Phil[/color]
                  >
                  > Hope it helps.
                  > Jimbo[/color]

                  Comment

                  Working...