To demonstrate parameter transfers between HTML pages, let’s create a go.html file with one line:

<a href="index.html?result=voted">Calling the site with parameters</a>

So the task is reduced to extracting variables and their values from a query (?result=voted). Of course, JavaScript is not the best programming language for handling such queries, but sometimes it is justified (e.g., when you cannot use Perl, PHP, etc.).

JavaScript has a property window.location.search or just location.search. To implement our task, the file index.html will contain the following text:

<script language='javascript'>

function getParam(sParamName)
// The function for defining the passed variable
{
var Params = location.search.substring(1).split("&"); 
// cut off the "?" and put the variables and their values into the array var variable = "";

for (var i = 0; i < Params.length; i++) // look through the entire variable array
  { 
        if (Params[i].split("=")[0] == sParamName) // if the desired variable is found, and
       { 
           if (Params[i].split("=").length > 1) variable = Params[i].split("=")[1]; 
           // if the value of the parameter is set, then 
           return variable; // We return it
       }
   }
   return "";
}

// Print result
str = getParam("result");
document.write('The value of the passed variable result = ');
document.write('<b>');
document.write(str);
document.write('</b>');

</script>

The basis of the contents of the index.html site is the getParam(sParamName) function, the call to which looks like this: getParam(“result”). In our example this function will output the string “voted”.

0 Shares:
Leave a Reply

Your email address will not be published.

You May Also Like