Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stripping + symbol from URL

Status
Not open for further replies.

tutterrow

Programmer
Feb 18, 2008
2
US
Hi,

I'm passing 3 from values to a page which I'm using top display a message based on these values. I need to strip out the + sign put into the url. I downloaded the code from tek-tips, and it's supposed to replace the + with a space, but in both the sample and my modified version the + sign is still being displayed. Original here

my code below.

<html>
<head>
<!-- head stuff -->
<script type="text/javascript">
<!-- hide from old browsers

function getValue(varname)
{
// First, we load the URL into a variable
var url = window.location.href;

// Next, split the url by the ?
var qparts = url.split("?");

// Check that there is a querystring, return "" if not
if (qparts.length == 0)
{
return "";
}

// Then find the querystring, everything after the ?
var query = qparts[1];

// Split the query string into variables (separates by &s)
var vars = query.split("&");

// Initialize the value with "" as default
var value = "";

// Iterate through vars, checking each one for varname
for (i=0;i<vars.length;i++)
{
// Split the variable by =, which splits name and value
var parts = vars.split("=");

// Check if the correct variable
if (parts[0] == varname)
{
// Load value into variable
value = parts[1];

// End the loop
break;
}
}

// Convert escape code
value = unescape(value);

// Convert "+"s to " "s
value.replace(/\+/g," ");

// Return the value
return value;
}

// end hide -->
</script>
</head>
<body>
<h1>Hello,
<script type="text/javascript">
<!-- hide
var name = getValue("name2");
document.write("$"+ name+ " transferred from ");
var name = getValue("name");
document.write(name+ " to ");
var name = getValue("name1");
document.write(name);

// end hide -->
</script>
</h1>
</body>
</html>
 
You need to reassign the value of value back to value (you could do with some more useful variable names, maybe [smile]).
Code:
		[!]value =[/!] value.replace(/\+/g," ");
An alternative would be to combine the last two lines into one:
Code:
		return value.replace(/\+/g," ");
Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top