JavaScript Instance Tutorial (a) JS instead of CGI

Source: Internet
Author: User
Tags variables script tag split variable
cgi|javascript|js| Tutorial

Use JavaScript instead of CGI

You might be familiar with CGI scripts, and you can use CGI to pass numbers or variables from one page to another on a form. Of course, you can use "POST" to pass a variable to a separate thread without appearing in the browser, or to encode the variable to a URL using "get".

JavaScript cannot use the Post method when a variable is processed on the server side, but you can note that when you submit a form using the Get method, the Anchor bar contains additional information after the file name, such as:

Www.pconline.com.cn/mypage.html?name=Mike&age=33

Using JavaScript We can access these variables, which can be implemented using a combination of related numbers, as follows:

<script language= "javascript1.1" >

<!--Hide from older browsers


function Forminput () {

Empty Object

}

var valueof = new Forminput ();

var pairs = document.location.search.substring (1). Split ("&");

for (Var i=0 i < pairs.length; i++) {

var values = pairs[i].split ("=");

Valueof[values[0]] = unescape (values[1]);

}

Stop hiding-->

</SCRIPT>

The purpose of the above code is to extract the variables from the browser's locator bar and store them in a related combination of numbers. In other parts of the page, any variable can be referenced like this: valueof["VariableName", of course, in the script tag.

As a test, we can use for. The In loop Loop statement lists all the variables that are passed. This allows us to loop over the contents of all the relevant arrays, regardless of whether we are not until the names of these variables. The specific code is as follows:

<script language= "JavaScript" >

<!--Hide from older browsers


for (var x in valueof) {

document.write (x + "=" + valueof[x] + "<br>");

}

Stop hiding-->

</SCRIPT>

This time you will enter the following content:

Name=mike

Age=33

This example is useless, but it's just to show you how the program works. These variables can now be used to populate the contents of a form element, or to display a picture or create other Web content, in short, to achieve what CGI can achieve.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.