fabyola
asked on
Separte a sting and put int variables
I have the following function:
<script language=JavaScript>
{
ctrl = window.location.search.sub str(8).sub string(5);
}
</script>
The "ctrl" value becomes "Equip&Descr=Descr"
I need to separate the string that´s in the "crl" into 2 more variables. I need that it reads "Equip" and put´s it in a variable and then reads "Descr" and put´s it in anther variable. How can I do this ???
<script language=JavaScript>
{
ctrl = window.location.search.sub
}
</script>
The "ctrl" value becomes "Equip&Descr=Descr"
I need to separate the string that´s in the "crl" into 2 more variables. I need that it reads "Equip" and put´s it in a variable and then reads "Descr" and put´s it in anther variable. How can I do this ???
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hehe, here's a nasty one:
<script>
var ctrl = "Equip&Descr=Description";
// Next line creates variable named Equip, and sets it = 'Description' !!
eval("var "+ctrl.split("&")[0]+"='"+ ctrl.split ("=")[1]+" '");
alert("Equip = "+Equip);
// Next line creates variable named Descr, and sets it = 'Description' !!
eval("var "+ctrl.split("=")[0].split ("&")[1]+" ='"+ctrl.s plit("=")[ 1]+"'");
alert("Descr = "+Descr);
</script>
Pro'ly not good programming practice. ;-)
Peace and joy. mvan
<script>
var ctrl = "Equip&Descr=Description";
// Next line creates variable named Equip, and sets it = 'Description' !!
eval("var "+ctrl.split("&")[0]+"='"+
alert("Equip = "+Equip);
// Next line creates variable named Descr, and sets it = 'Description' !!
eval("var "+ctrl.split("=")[0].split
alert("Descr = "+Descr);
</script>
Pro'ly not good programming practice. ;-)
Peace and joy. mvan
or you can try this function:
function getParameter ( queryString, parameterName ) {
//Use like UniqueID = getParameter ( location.search , "uid" );
// Add "=" to the parameter name (i.e. parameterName=value)
var parameterName = parameterName + "=";
if ( queryString.length > 0 ) {
// Find the beginning of the string
begin = queryString.indexOf ( parameterName );
// If the parameter name is not found, skip it, otherwise return the value
if ( begin != -1 ) {
// Add the length (integer) to the beginning
begin += parameterName.length;
// Multiple parameters are separated by the "&" sign
end = queryString.indexOf ( "&" , begin );
if ( end == -1 ) {
end = queryString.length;
}
// Return the string
return unescape ( queryString.substring ( begin, end ) );
}
// Return "null" if no parameter has been found
return "null";
}
}
use it like:
alert("Equip = "+getParameter(location.se arch, "Equip"));
alert("Descr = "+getParameter(location.se arch, "Descr"));
hope this helps too, cheers
function getParameter ( queryString, parameterName ) {
//Use like UniqueID = getParameter ( location.search , "uid" );
// Add "=" to the parameter name (i.e. parameterName=value)
var parameterName = parameterName + "=";
if ( queryString.length > 0 ) {
// Find the beginning of the string
begin = queryString.indexOf ( parameterName );
// If the parameter name is not found, skip it, otherwise return the value
if ( begin != -1 ) {
// Add the length (integer) to the beginning
begin += parameterName.length;
// Multiple parameters are separated by the "&" sign
end = queryString.indexOf ( "&" , begin );
if ( end == -1 ) {
end = queryString.length;
}
// Return the string
return unescape ( queryString.substring ( begin, end ) );
}
// Return "null" if no parameter has been found
return "null";
}
}
use it like:
alert("Equip = "+getParameter(location.se
alert("Descr = "+getParameter(location.se
hope this helps too, cheers
Here's another way. Say you have:
var ctrl = "Equip&Descr=Description";
You can do this:
var v1 = ctrl.split("=");
Now, v1[0]="Equip&Descr", and v1[1]="Description"
Then, you can do this:
var v2 = v1[0].split("&");
Of course, now v2[0]="Equip" and v2[1]="Descr" .
Put it all together and here it is:
var s1=ctrl.split("&")[0]; // <-- should be "Equip"
var s2=ctrl.split("&")[1].spli
var s3=ctrl.split("=")[1]; // <-- should be "Description"
Peace and joy. mvan