Passing parameters to an other page in VisualForce using JavaScript
Osama | October 22, 2009There is a general requirement of passing parameters from one page to another through queryString using GET Method. The same thing was required in a project. Below is the code showing ho did I do that in Visual Force page using apex.
This is the page FROM which the parameter has to be sent
<apex:page> <body><form name="myForm" method="get" action="nextPageURL"> <input type="hidden" value="SampleValue" name="name"/> <input type="submit" value="Submit"/></body> </apex:page>
According to this code, there is a hidden input with value “SampleValue” and when we submit, this value is passed to next page in query string. The url would look like “nextPageURL?name=SampleValue.
The code blow how to retrieve the receved paramter. The code below has to be in the other page.
<apex:page>
<head>
<script type="text/javascript">
function getValue(varname)
{
var url = window.location.href;
var qparts = url.split("?");
if (qparts.length == 0)
{
return "";
}
var query = qparts[1];
var vars = query.split("&");
var value = "";
for (i=0;i<vars.length;i++)
{
var parts = vars[i].split("=");
if (parts[0] == varname)
{
value = parts[1];
break;
}
}
value = unescape(value);
value.replace(/\+/g," ");
return value;
}
</script>
</head>
<body>
<script type="text/javascript">
var name = getValue("name");
alert(name);
</body>
</apex:page>
The getValue() method search for “name” and decrypt the received paramter from the query string. It worked for me. Feel free to ask any questions









