Welcome to WebmasterWorld Guest from 35.153.135.60

Forum Moderators: open

Message Too Old, No Replies

Javascript to edit document.location before submit

javascript replace

     
5:27 pm on Feb 25, 2012 (gmt 0)

New User

5+ Year Member

joined:June 27, 2011
posts: 9
votes: 0


Good day,

I have a javascript function which isn't quite working the way I want it.

I want a seperate submit button on a search page, that will allow me to compare the production results with the test environment.

I thought the best way to do so, would be to have a javascript function on the 2nd submit button to edit the URL of document.location to point to production then submit.

So the function looks like:

var url=document.location;
url.replace(/test\./, "");
alert("Am at " + url);


What it is supposed to do is format a URL from this:

<username>.<projectnumber>.<employeename>.db.test.domain.com/restOfURL

to

<username>.<projectnumber>.<employeename>.db.domain.com/restOfURL

But the code is replacing the "restOfURL" part.

I will need the
<username>.<projectnumber>.<employeename>. parts removed as well, but so far I will settle for getting the "test." part removed.

What would be better to use for the replace?
6:25 pm on Feb 25, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts:5021
votes: 26


Try this:
url.replace("test", "");
6:46 pm on Feb 25, 2012 (gmt 0)

New User

5+ Year Member

joined:June 27, 2011
posts: 9
votes: 0



I'm still getting the same behaviour, the domain name is not updating. So only parts after the first '/' get changed...

what the hell javascript?!?
11:11 pm on Feb 25, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3127
votes: 1


The replace method doesn't change the string, it returns a new string...

url = url.replace(/test\./, "");
11:13 pm on Feb 25, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts:5021
votes: 26


Sorry, you need to assign the value back to url. Also, you need to call toString() on the url (since it's actually a location object).

url = url.toString().replace("test.", "");
6:11 am on Feb 26, 2012 (gmt 0)

New User

5+ Year Member

joined:June 27, 2011
posts: 9
votes: 0


Thanks everyone, this code is working. I had url=url.replace(...) before, but I did not realize that it is a location object vs. a string.

One final assist please:

Once I have my new URL, how can I make the form submit on that?

 document.formName.action = newUrl? 
11:43 am on Feb 26, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3127
votes: 1


Actually, wouldn't it be more usual to refer to (window.)location.href rather than document.location? window.location is part of the standard, but I didn't think document.location was?

var url = window.location.href; 
url = url.replace("test.", "");