简体   繁体   中英

Javascript file for multiple html pages

I am beginner in Javascript. I am currentlyworking on a Phonegap app with it. I am stuck in between as I have 4 html pages for signup process, and I have to pass all the html pages input value to single js file as in final all data must be POSTed to server URL and also I have read on many sites that they have recommended using same js file for all the pages of your site to speed up the site. So I have two problems to solve. I searched on many sites but could not find the accurate answer.

  1. I need to pass 4 html page's input value to single js file.

  2. I have to make single js file for both sign-in and sign-up.

My codes for JS page is:

var firstName="";
var lastName="";
var email="";
var password="";
var retypePassword="";
var gender="";
var DOB="";
var institute="";
var course="";
var branch="";
var semester="";
var teachers = [];

function signUpStarting() {
    alert(firstName + " "+lastName+" "+email+" "+password+" "+retypePassword+" "+gender+" "+DOB+" "+institute+" "+course+" "+branch+" "+semester+" "+teachers.join(","));

}   

function signUp1() {
    firstName[0] = $("#first_name").val().trim();
    firstName[1] = $("#last_name").val().trim();
    email = $("#email").val().trim();
    password = $("#password").val();
    retypePassword = $("#retype_password").val();   
    alert(firstName + " "+lastName+" "+email+" "+password+" "+retypePassword);
}

function signUp2() {
    gender = $('#gender').find(":selected").text();
    DOB = $('#DOB').val();
    alert(gender+" "+DOB);
}

function signUp3() {
    institute = $('#institute').find(":selected").text();
    course = $('#course').find(":selected").text();
    branch = $('#branch').find(":selected").text();
    semester = $('#semester').find(":selected").text();
    alert(institute+" "+course+" "+branch+" "+semester);
}

function signUp4() {
    $(":checkbox" ).map(function() {
            if($(this).is(':checked')){
                    teachers.push($('label[for="' + this.id + '"]').text());
            }
    });
    signUpStarting();
}

In html pages I am calling JS functions for each pages:

On first page:

<a onclick="signUp1()" href="register-two.html">continue</a>

On second page:

<a onclick="signUp2()" href="register-three.html">continue</a>

On third page:

<a onclick="signUp3()" href="register-four.html">continue</a>

On fourth page:

<a onclick="signUp4()">continue</a>

On each transaction from one page to next I have set alert in JS, and I am getting alert with accurate values also. But after clicking the continue button from fourth page of html, I transferred the code to main signup function. I tried to see alert in signUpStarting() function but there I am getting response of just fourth page values and other values are showing nothing as the variables are null.

I am not getting how to save variable values for always without using localStorage or cookies and POSTing all data to server.And I think this would have been easier if I would know to code for all html pages for my site to single JS file.

Please help me !

I am not getting how to save variable values for always without using localStorage or cookies and POSTing all data to server.And I think this would have been easier if I would know to code for all html pages for my site to single JS file.

This is exactly right. You cannot store data in memory between page loads in a web browser environment because all javascript variables are naturally destroyed when the browser navigates away from the page to a new page (even if they use the same javascript on both pages). Thus, you have to save it somewhere with more permanence: localStorage, cookies, or on the server via POST or GET.

What I would recommend is scrapping the four different html pages and simply using one html page that changes dynamically as the user fills in data. This way the browser will not eliminate data before you are ready to POST it to the server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM