Click to See Complete Forum and Search --> : Pass variables
Hi all,
Is there anyway to pass a string variable from the first page (form) to another page (js page)..
like asp: Request.QueryString(----)
?
Thanks in advance,
zeid
toicontien
12-05-2006, 10:05 AM
What you're saying doesn't quite make sense. Pages are HTML. They contain forms and JavaScript. Are you trying to submit a form and have that variable available on the page that loads after submitting the form? I can think of two ways to accomplish this:
1) Set a cookie
2) Since you are submitting a form, use the GET method so the variable-value pairs appear in the window.location JavaScript variable. Then you can parse the window location for the '?', and grab the string from that point on to the end of the window location. Then split that string at the '&' characters, and split them again at the '=' characters. Furthermore, you can use the javascript unescape() function to change the URL-escaped data to normal characters, i.e. %20 in a URL is unescaped to the space character ' '.
I did to pass it with the url but its not working.. I will be thankfull is someone can show it to me .. this is my code:
HTML (page1.html)
<body>
<br /><br/>
<form name="F1" action="Search.html" method="get">
<center>
<input type="text" name="search" size="35" />
<input type="submit" value="Search JU" />
</center>
</form>
</body>
and lets say this is (page2.html) where I have my script:
<script type="text/javascript">
var search1 = ???;
so how can I reveive the variable from page1.html
Thanks in advance,
zeid
sovik
12-05-2006, 12:05 PM
Somebody else example:
<script>
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
alert('Query Variable ' + variable + ' not found');
}
</script>
Now make a request to page.html?x=Hello
<script>
alert( getQueryVariable("x") );
</script>
ref: http://www.activsoftware.com/code_samples/code.cfm/CodeID/59/JavaScript/Get_Query_String_variables_in_JavaScript
toicontien
12-05-2006, 12:10 PM
I've used this code before:
var _GET = [];
function readGET() {
var tLoc = "", tPairs = "";
var tGet = [];
var foundGet = -1;
tLoc = window.location + "";
foundGet = tLoc.indexOf('?');
if (foundGet > -1) {
tLoc = tLoc.substring(foundGet + 1, tLoc.length);
tPairs = tLoc.split('&');
for (var i = 0; i < tPairs.length; i++) {
tGet = tPairs[i].split('=');
_GET[tGet[0]] = unescape(tGet[1]);
}
}
}
readGET();
Then you can access the variables and values in the URL just like you would in PHP:
// Assume this URL: example.php/?name=Fred%20Flintstone&title=Caveman
alert(_GET['name']);
alert(_GET['title']);
You can also use JavaScript dot-notation, since _GET is really an object:
// Assume this URL: example.php/?name=Fred%20Flintstone&title=Caveman
alert(_GET.name);
alert(_GET.title);
What you're saying doesn't quite make sense. Pages are HTML. They contain forms and JavaScript. Are you trying to submit a form and have that variable available on the page that loads after submitting the form? I can think of two ways to accomplish this:
1) Set a cookie
2) Since you are submitting a form, use the GET method so the variable-value pairs appear in the window.location JavaScript variable. Then you can parse the window location for the '?', and grab the string from that point on to the end of the window location. Then split that string at the '&' characters, and split them again at the '=' characters. Furthermore, you can use the javascript unescape() function to change the URL-escaped data to normal characters, i.e. %20 in a URL is unescaped to the space character ' '.
You forgot the third way (a variant of your 2nd way). It can be done in javascript as well, by opening the new page with javascript methods and simply adding a query to the location.