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

P

Phil Powell

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
 
M

McKirahan

Phil Powell said:
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);
 
J

J. J. Cale

Phil Powell said:
Code:
if (document.location.href.indexOf('?') >= 0) document.location.href
= document.location.href.substring(0,
document.location.href.indexOf('?'));[/QUOTE]

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>
[QUOTE]
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
 
M

McKirahan

Philip Ronan said:
Am I missing something here?

What's wrong with "location.pathname"?


Don't you mean:

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

Philip Ronan

McKirahan said:
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?
 
M

McKirahan

Philip Ronan said:
Uhh, yeah. If you want an absolute URL.

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

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

Phil Powell

Philip Ronan said:
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
 
P

Phil Powell

J. J. Cale said:
Phil Powell said:
Code:
if (document.location.href.indexOf('?') >= 0) document.location.href
= document.location.href.substring(0,
document.location.href.indexOf('?'));[/QUOTE]

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.[/QUOTE]

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top