faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

13 of 15 people (87%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

Is there any way to keep a prompt variable active so that it can be used in a different html pages?

Apr 5th, 2008 19:26
ha mo, Dave Clark, Tamara Fendez,


Tamara,
The easiest way is to pass the variable as a query string attachment on 
the URL for the next page.  For example...  If you were going to 
navigate immediately to another HTML page, and you wanted to prompt for 
information to pass along to that page, you could do that like this:
<script language="JavaScript">
<!-- // Begins
var url = "nextpage.html";
var pass = "";
while (pass <= " ") {
   pass = prompt("Enter required information:", "");
}
self.location.href = url + "?pass=" + escape(pass);
// Ends -->
</script>
The next page would then extract this information for use within that 
page.  The following code is a souped-up version of what would actually 
be required for this simple case.  However, the advantage here is that 
the following code will work for *all* query string needs -- no matter 
how complex (quite a claim, eh?):
<script language="JavaScript">
<!-- // Begins
var i, inp = self.location.search.substr(1);
var queryString = new Object();
if (inp.length > 0) {
   var ary = inp.replace(/\+/g, " ").split("&");
   for (i in ary) {
      ary[i] = unescape(ary[i]).split("=");
      queryString[ary[i][0]] = ary[i][1];
   }
}
// Ends -->
</script>
After the above code is executed, the data from the previous page (in 
this example) is contained in the following object reference:
queryString["pass"]
As I alluded earlier, any number of variables may be passed in this 
manner.  The extract code, I supplied, will automatically and 
faithfully extract any number of variables from the query string.  For 
example, if the following URL is used:
http://www.domain.com/somepage.html?a1=first&a2=second&a3=third
then the following three references will be valid after that extract 
code has executed:
queryString["a1"]
queryString["a2"]
queryString["a3"]
OK?   ;-)
Take care,
Dave Clark
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com