473,698 Members | 2,304 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I always chop off both query string and "#" from document.locati on.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.locat ion.href" and chop off the query
string and "#" before it redirects in Javascript.

Thanx
Phil
Jul 23 '05 #1
8 8231
"Phil Powell" <ph************ **@gmail.com> wrote in message
news:b5******** *************** ***@posting.goo gle.com...
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.locat ion.href" and chop off the query
string and "#" before it redirects in Javascript.

Thanx
Phil


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

"Phil Powell" <ph************ **@gmail.com> wrote in message
news:b5******** *************** ***@posting.goo gle.com...
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.locat ion.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.pathn ame"?

    --
    Philip Ronan
    ph***********@v irgin.net
    (Please remove the "z"s if replying by email)
    Jul 23 '05 #4
    "Philip Ronan" <ph***********@ virgin.net> wrote in message
    news:BD9AE7DD.2 406E%ph******** ***@virgin.net. ..
    Am I missing something here?

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

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

    Don't you mean:

    location.protoc ol + "//" + location.host + location.pathna me
    Jul 23 '05 #5
    McKirahan wrote:
    "Philip Ronan" <ph***********@ virgin.net> wrote in message
    news:BD9AE7DD.2 406E%ph******** ***@virgin.net. ..

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


    Don't you mean:

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


    Uhh, yeah. If you want an absolute URL.

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

    --
    Philip Ronan
    ph***********@v irgin.net
    (Please remove the "z"s if replying by email)
    Jul 23 '05 #6
    "Philip Ronan" <ph***********@ virgin.net> wrote in message
    news:BD9AF2C8.2 4083%ph******** ***@virgin.net. ..
    McKirahan wrote:
    "Philip Ronan" <ph***********@ virgin.net> wrote in message
    news:BD9AE7DD.2 406E%ph******** ***@virgin.net. ..

    What's wrong with "location.pathn ame"?
    Don't you mean:

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


    Uhh, yeah. If you want an absolute URL.

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

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


    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.
    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.pathn ame"?


    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****@netvisi on.net.il> wrote in message news:<41******* *@news.012.net. il>...
    "Phil Powell" <ph************ **@gmail.com> wrote in message
    news:b5******** *************** ***@posting.goo gle.com...
    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.locat ion.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
    3339
    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 is now restricted by the simple "Between And ". What I want to do is pass the values of the two parameters into some textboxes in the heading of the report (so if 12/1/03 and 12/31/03 are the parameters, I can display the entered dates in the...
    3
    5242
    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 interop assembly, put them both in the GAC. On my main development machine plugins work OK. On this new machine no plugins work (not even the ToolWindow example). I keep getting this error message when invoking EnvDTE.Windows.CreateToolWindow: ...
    10
    3949
    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 seems that I can add one and only one record to the table, any other attempts to insert a record generate the error: "Operation Must Use An Updatable Query" I can access the table via other means, delete that one record, try again, same...
    4
    1986
    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 string.length property). String's contents seem be teh same, so I think there is some encoding difference (is this possibile?). The problem is that I sign this string, so signature validation fails :( What can be the problem?
    3
    5028
    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 it works, so why is it that about once a week for about half an hour I get this error? Any Ideas? Thanks, Ady
    4
    15756
    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 table (on the keys) they are replaced/updated by the new data in the temp table. the vba code i am running is below: Dim mySQL As String
    7
    3084
    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"> function disp_confirm(employeenominationid) {
    8
    4432
    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 Server environments, but I'm a little rusty, plus I'm very new to Access. I am trying to perform a calculation based on information in a few tables, and use the result to update another. Should be simple, here's what counts from the tables...
    1
    8675
    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 = from staff in Database.Staff where staff.Value.Passcode == txt_password.Text select staff;but i get the error Error 2 Cannot implicitly convert type...
    7
    4439
    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 surrendered to you. <%@ Language=VBScript%> <%Option Explicit%>
    0
    9157
    Oralloy
    by: Oralloy | last post by:
    Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
    1
    8895
    by: Hystou | last post by:
    Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
    0
    8861
    tracyyun
    by: tracyyun | last post by:
    Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
    0
    7725
    agi2029
    by: agi2029 | last post by:
    Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
    0
    5860
    by: conductexam | last post by:
    I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
    0
    4369
    by: TSSRALBI | last post by:
    Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
    0
    4619
    by: adsilva | last post by:
    A Windows Forms form does not have the event Unload, like VB6. What one acts like?
    2
    2329
    muto222
    by: muto222 | last post by:
    How can i add a mobile payment intergratation into php mysql website.
    3
    2001
    bsmnconsultancy
    by: bsmnconsultancy | last post by:
    In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

    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.