Skip to main content

Class 5

Class 5 Keypoint:

Using GetUrlVar to get variables from other HTML file pages.

Page A can open Page B with variables as input for page B, so page B can show different content for each input.
For this we use the following function inside page B: 
function GetUrlVar(VarName)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + VarName + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null)
return "";
else
return results[1];
}
var filmid = decodeURIComponent(GetUrlVar('filmid')); var numID= Number(filmid);
document.getElementById("demo").innerHTML = writeText(numID);
    


Where filmid is the variable to store variable from URL, but it is saved as char string, to cast to integer we create numID and cast to an integer with Number();

And in page A we open page B (items.html) as follow:
        "items.html?filmid=x", where x is the id number to pass to page B.


Homework:

















Comments