By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,534 Members | 1,807 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,534 IT Pros & Developers. It's quick & easy.

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

P: n/a
Expand|Select|Wrap|Line Numbers
  1. if (document.location.href.indexOf('?') >= 0) document.location.href
  2. = document.location.href.substring(0,
  3. document.location.href.indexOf('?'));
  4.  
  5. if (document.location.href.indexOf('#') >= 0) {
  6. document.location.href = document.location.href.substring(0,
  7. document.location.href.indexOf('#')) + '?' + newUrl;
  8. } else {
  9. document.location.href = document.location.href + '?' + newUrl;
  10. }
  11.  
  12.  
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
Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"Phil Powell" <ph**************@gmail.com> wrote in message
news:b5**************************@posting.google.c om...
Expand|Select|Wrap|Line Numbers
  1.  if (document.location.href.indexOf('?') >= 0) document.location.href
  2.  = document.location.href.substring(0,
  3.  document.location.href.indexOf('?'));
  4.  if (document.location.href.indexOf('#') >= 0) {
  5.  document.location.href = document.location.href.substring(0,
  6.  document.location.href.indexOf('#')) + '?' + newUrl;
  7.  } else {
  8.  document.location.href = document.location.href + '?' + newUrl;
  9.  }
  10.  

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);
Jul 23 '05 #2

P: n/a

"Phil Powell" <ph**************@gmail.com> wrote in message
news:b5**************************@posting.google.c om...
Expand|Select|Wrap|Line Numbers
  1.  if (document.location.href.indexOf('?') >= 0) document.location.href
  2.  = document.location.href.substring(0,
  3.  document.location.href.indexOf('?'));
  •  
  • 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>
  •  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
    Jul 23 '05 #3

    P: n/a
    Am I missing something here?

    What's wrong with "location.pathname"?

    --
    Philip Ronan
    ph***********@virgin.net
    (Please remove the "z"s if replying by email)
    Jul 23 '05 #4

    P: n/a
    "Philip Ronan" <ph***********@virgin.net> wrote in message
    news:BD9AE7DD.2406E%ph***********@virgin.net...
    Am I missing something here?

    What's wrong with "location.pathname"?

    --
    Philip Ronan
    ph***********@virgin.net
    (Please remove the "z"s if replying by email)

    Don't you mean:

    location.protocol + "//" + location.host + location.pathname
    Jul 23 '05 #5

    P: n/a
    McKirahan wrote:
    "Philip Ronan" <ph***********@virgin.net> wrote in message
    news:BD9AE7DD.2406E%ph***********@virgin.net...

    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
    ph***********@virgin.net
    (Please remove the "z"s if replying by email)
    Jul 23 '05 #6

    P: n/a
    "Philip Ronan" <ph***********@virgin.net> wrote in message
    news:BD9AF2C8.24083%ph***********@virgin.net...
    McKirahan wrote:
    "Philip Ronan" <ph***********@virgin.net> wrote in message
    news:BD9AE7DD.2406E%ph***********@virgin.net...

    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
    ph***********@virgin.net
    (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.
    Jul 23 '05 #7

    P: n/a
    Philip Ronan <ph***********@virgin.net> wrote in message news:<BD9AE7DD.2406E%ph***********@virgin.net>...
    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
    Jul 23 '05 #8

    P: n/a
    "J. J. Cale" <ph****@netvision.net.il> wrote in message news:<41********@news.012.net.il>...
    "Phil Powell" <ph**************@gmail.com> wrote in message
    news:b5**************************@posting.google.c om...
    Expand|Select|Wrap|Line Numbers
    1.  > if (document.location.href.indexOf('?') >= 0) document.location.href
    2.  > = document.location.href.substring(0,
    3.  > document.location.href.indexOf('?'));
  •  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.
  •  
  • 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.
  •  
  •     Expand|Select|Wrap|Line Numbers
  •  
  •     
  •  
  • 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;
  •  > }
  •  >
  •  > 

  • 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
    Jul 23 '05 #9

    This discussion thread is closed

    Replies have been disabled for this discussion.