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

Discussion in 'Javascript' started by Phil Powell, Oct 18, 2004.

  1. Phil Powell

    Phil Powell Guest

    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.location.href" and chop off the query
    string and "#" before it redirects in Javascript.

    Thanx
    Phil
    Phil Powell, Oct 18, 2004
    #1
    1. Advertising

  2. Phil Powell

    McKirahan Guest

    "Phil Powell" <> wrote in message
    news:...
    >
    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.location.href" and chop off the query
    > string and "#" before it redirects in Javascript.
    >
    > Thanx
    > Phil


    Will this work for you?

    var href = document.location.href;
    href = href.substring(0,href.indexOf("?"));
    href = href.substring(0,href.indexOf("#"));
    alert(href);
    McKirahan, Oct 18, 2004
    #2
    1. Advertising

  3. Phil Powell

    J. J. Cale Guest

    "Phil Powell" <> wrote in message
    news:...
    >
    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.location.href" and chop off the query
    > string and "#" before it redirects in Javascript.
    >
    > Thanx
    > Phil


    Hope it helps.
    Jimbo
    J. J. Cale, Oct 19, 2004
    #3
  4. Phil Powell

    Philip Ronan Guest

    Re: How do I always chop off both query string and "#" fromdocument.location.href?

    Am I missing something here?

    What's wrong with "location.pathname"?

    --
    Philip Ronan

    (Please remove the "z"s if replying by email)
    Philip Ronan, Oct 19, 2004
    #4
  5. Phil Powell

    McKirahan Guest

    Re: How do I always chop off both query string and "#" fromdocument.location.href?

    "Philip Ronan" <> wrote in message
    news:BD9AE7DD.2406E%...
    > Am I missing something here?
    >
    > What's wrong with "location.pathname"?
    >
    > --
    > Philip Ronan
    >
    > (Please remove the "z"s if replying by email)
    >



    Don't you mean:

    location.protocol + "//" + location.host + location.pathname
    McKirahan, Oct 19, 2004
    #5
  6. Phil Powell

    Philip Ronan Guest

    Re: How do I always chop off both query string and "#"fromdocument.location.href?

    McKirahan wrote:

    > "Philip Ronan" <> wrote in message
    > news:BD9AE7DD.2406E%...
    >>
    >> What's wrong with "location.pathname"?

    >
    > Don't you mean:
    >
    > location.protocol + "//" + location.host + location.pathname


    Uhh, yeah. If you want an absolute URL.

    What's the purpose of all this "document.location.href.indexOf('?')" stuff?

    --
    Philip Ronan

    (Please remove the "z"s if replying by email)
    Philip Ronan, Oct 19, 2004
    #6
  7. Phil Powell

    McKirahan Guest

    Re: How do I always chop off both query string and "#"fromdocument.location.href?

    "Philip Ronan" <> wrote in message
    news:BD9AF2C8.24083%...
    > McKirahan wrote:
    >
    > > "Philip Ronan" <> wrote in message
    > > news:BD9AE7DD.2406E%...
    > >>
    > >> What's wrong with "location.pathname"?

    > >
    > > Don't you mean:
    > >
    > > location.protocol + "//" + location.host + location.pathname

    >
    > Uhh, yeah. If you want an absolute URL.
    >
    > What's the purpose of all this "document.location.href.indexOf('?')"

    stuff?
    >
    > --
    > Philip Ronan
    >
    > (Please remove the "z"s if replying by email)
    >


    I screwed up. It should have been:

    var href = document.location.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.
    McKirahan, Oct 19, 2004
    #7
  8. Phil Powell

    Phil Powell Guest

    Philip Ronan <> wrote in message news:<BD9AE7DD.2406E%>...
    > Am I missing something here?
    >
    > What's wrong with "location.pathname"?


    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
    Phil Powell, Oct 27, 2004
    #8
  9. Phil Powell

    Phil Powell Guest

    "J. J. Cale" <> wrote in message news:<41752392$1@news.012.net.il>...
    > "Phil Powell" <> wrote in message
    > news:...
    > >
    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

    >
    > <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>
    >
    > > 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;
    > > }
    > >
    > > [/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.location.href" and chop off the query
    > > string and "#" before it redirects in Javascript.
    > >
    > > Thanx
    > > Phil

    >
    > Hope it helps.
    > Jimbo
    Phil Powell, Oct 27, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johnathan Smith

    chop and chop!

    Johnathan Smith, Jan 8, 2008, in forum: Ruby
    Replies:
    2
    Views:
    132
    darren kirby
    Jan 8, 2008
  2. saiho.yuen
    Replies:
    3
    Views:
    416
    kaeli
    Sep 14, 2004
  3. yusufm

    print chop; VS chop; print;

    yusufm, Mar 9, 2006, in forum: Perl Misc
    Replies:
    2
    Views:
    111
    Tad McClellan
    Mar 9, 2006
  4. martin
    Replies:
    3
    Views:
    177
    Joe Smith
    Apr 15, 2006
  5. Replies:
    2
    Views:
    457
Loading...

Share This Page