Passing Variables in URLs |
Active Web |
Sometimes it is useful to pass hidden variables from a page back to the script program when a URL is used rather than a form. Using the ‘query string’ format in a URL can do this. What you do is include the variables as name value pairs separated by ampersand characters. If you use the same name for two or more variables then it will be seen as an array by the server-side script.
The example below shows an ‘anchor’ tag which has a ‘query string’ attached. The ‘query string’ must start with a question mark, equals signs separate name and values and name/value pairs are separated by ampersands. e.g.
<a href=”verfiyLogin.html?username=fred&password=derf&loginflag=true”>default login</a>
There are a number of characters that must not be put into a ‘query string’ including space. In order to get round this there is a standard encoding method for query strings that solves all the ‘illegal’ characters problems. The Global object has various methods that will encode the ‘query string’. The example below shows a URL being built by a script with an escaped ‘query string’.
It is recommended that ‘query strings’ are always encoded even if they don’t need it right now. e.g.
<script language=”javascript” runat=”server”>
write(“<a href=\”verfiyLogin.html?”+encodeURI(“username=fred smith&password=derf htims&loginflag=true”)+”\””);
writeln(“default login</a>”);
</script>
The quotes inside the URL must have the escape character (\) in front of them or the script server will take them as the end of the string. |
Topic ID: 150085