Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How do I always chop off both query string and "#" from document.location.href?

Reply
Thread Tools

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

 
 
Phil Powell
Guest
Posts: n/a
 
      10-18-2004
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
 
Reply With Quote
 
 
 
 
McKirahan
Guest
Posts: n/a
 
      10-18-2004
"Phil Powell" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
>
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);


 
Reply With Quote
 
 
 
 
J. J. Cale
Guest
Posts: n/a
 
      10-19-2004

"Phil Powell" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
>
Code:
> if (document.location.href.indexOf('?') >= 0) document.location.href
> = document.location.href.substring(0,
> document.location.href.indexOf('?'));
Code:
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


 
Reply With Quote
 
Philip Ronan
Guest
Posts: n/a
 
      10-19-2004
Am I missing something here?

What's wrong with "location.pathname"?

--
Philip Ronan
http://www.velocityreviews.com/forums/(E-Mail Removed)
(Please remove the "z"s if replying by email)


 
Reply With Quote
 
McKirahan
Guest
Posts: n/a
 
      10-19-2004
"Philip Ronan" <(E-Mail Removed)> wrote in message
news:BD9AE7DD.2406E%(E-Mail Removed)...
> Am I missing something here?
>
> What's wrong with "location.pathname"?
>
> --
> Philip Ronan
> (E-Mail Removed)
> (Please remove the "z"s if replying by email)
>



Don't you mean:

location.protocol + "//" + location.host + location.pathname


 
Reply With Quote
 
Philip Ronan
Guest
Posts: n/a
 
      10-19-2004
McKirahan wrote:

> "Philip Ronan" <(E-Mail Removed)> wrote in message
> news:BD9AE7DD.2406E%(E-Mail Removed)...
>>
>> 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
(E-Mail Removed)
(Please remove the "z"s if replying by email)


 
Reply With Quote
 
McKirahan
Guest
Posts: n/a
 
      10-19-2004
"Philip Ronan" <(E-Mail Removed)> wrote in message
news:BD9AF2C8.24083%(E-Mail Removed)...
> McKirahan wrote:
>
> > "Philip Ronan" <(E-Mail Removed)> wrote in message
> > news:BD9AE7DD.2406E%(E-Mail Removed)...
> >>
> >> 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
> (E-Mail Removed)
> (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.


 
Reply With Quote
 
Phil Powell
Guest
Posts: n/a
 
      10-27-2004
Philip Ronan <(E-Mail Removed)> wrote in message news:<BD9AE7DD.2406E%(E-Mail Removed)>...
> 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
 
Reply With Quote
 
Phil Powell
Guest
Posts: n/a
 
      10-27-2004
"J. J. Cale" <(E-Mail Removed)> wrote in message news:<41752392$1@news.012.net.il>...
> "Phil Powell" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> >
Code:
> > if (document.location.href.indexOf('?') >= 0) document.location.href
> > = document.location.href.substring(0,
> > document.location.href.indexOf('?'));
Code:
>
> 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.
Code:
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; > > } > > > >

> >
> > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
chop and chop! Johnathan Smith Ruby 2 01-08-2008 04:54 PM
"chomp,chop" usage i.e. chop immediately after chomp martin Perl Misc 3 04-15-2006 08:09 PM
print chop; VS chop; print; yusufm Perl Misc 2 03-09-2006 12:29 AM
best way to chop off leading char in string? Aaron Walker C Programming 3 11-22-2003 10:19 PM



Advertisments