473,372 Members | 997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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

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
8 8183
"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

"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
    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
    "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
    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
    "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
    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
    "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 thread has been closed and replies have been disabled. Please start a new discussion.

    Similar topics

    4
    by: Greg Iocco | last post by:
    Simple problem, but I've checked the newsgroups and couldn't find an answer. On the main swithboard is a command button to open a report. The report I built is based off a query, and the query...
    3
    by: Paul | last post by:
    Help, please, I can't get any addins working on a new machine. I registered the correct plugin names into the right registry location. I gave VSControlHostLib with a strong name, created the...
    10
    by: cyber0ne | last post by:
    I'm having a bit of trouble adding records to a local .DB file (using a Paradox connection string). I checked the file permissions, and for testing purposes set Everyone to Full access. But it...
    4
    by: Trapulo | last post by:
    I've a webservice with a string parameter. I call this webservice passing a string that is 1129 chars length. On the webservice, the received string is 1146 length (I tested sizes with...
    3
    by: Ady1 | last post by:
    Hi, I'm getting this error intermitantly in a custom configuration section in my ASP.NET website. From what I can see 0.175 is a valid string to be converted to a decimal! And most of the time...
    4
    by: Mike D | last post by:
    OS: Windows XP Professional Microsoft Access 2003 I am trying to update a table in my DB from a tempory table. I need to ensure that if records in the main table match records in the temp...
    7
    by: preeti13 | last post by:
    i have a java script function with the pop up i want to update a database ow i am getting the erorr any one please help me with this. code is like this <script type="text/javascript"> ...
    8
    geolemon
    by: geolemon | last post by:
    I'm trying to perform an update, and I can't avoid this error - I've tried this query what must be 5 different very fundamentally different ways now. Arg. I used to be a DBA in large DB2 and SQL...
    1
    by: alex21 | last post by:
    Ok i am trying to use a Linq query to access a dictionary. public static Dictionary<string, Client> Clients = new Dictionary<string, Client>();Using this Linq query: IEnumerable<Staff> loginquery...
    7
    vikas251074
    by: vikas251074 | last post by:
    I am getting error above in following code since few days giving tension day and night. How can I solve this? I am facing since Oct.25. in line no. 362 After doing a lot of homework, I am...
    1
    by: CloudSolutions | last post by:
    Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
    0
    by: Faith0G | last post by:
    I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
    0
    isladogs
    by: isladogs | last post by:
    The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
    0
    by: Charles Arthur | last post by:
    How do i turn on java script on a villaon, callus and itel keypad mobile phone
    0
    by: aa123db | last post by:
    Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
    0
    by: ryjfgjl | last post by:
    If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
    1
    by: nemocccc | last post by:
    hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
    1
    by: Sonnysonu | last post by:
    This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
    0
    by: Hystou | last post by:
    There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

    By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

    To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.