简体   繁体   English

多个html页面的Javascript文件

[英]Javascript file for multiple html pages

I am beginner in Javascript.我是 Javascript 的初学者。 I am currentlyworking on a Phonegap app with it.我目前正在使用它开发 Phonegap 应用程序。 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.我被困在中间,因为我有 4 个 html 页面用于注册过程,我必须将所有 html 页面输入值传递给单个 js 文件,因为最终所有数据都必须发布到服务器 URL,而且我已经在许多网站上阅读了他们建议对您网站的所有页面使用相同的 js 文件以加快网站速度。 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.我需要将 4 个 html 页面的输入值传递给单个 js 文件。

  2. I have to make single js file for both sign-in and sign-up.我必须为登录和注册制作单个 js 文件。

My codes for JS page is:我的 JS 页面代码是:

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:在 html 页面中,我为每个页面调用 JS 函数:

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.在从一页到下一页的每笔交易中,我都在 JS 中设置了警报,并且我也收到了准确值的警报。 But after clicking the continue button from fourth page of html, I transferred the code to main signup function.但是在点击html第四页的继续按钮后,我将代码转移到主注册功能。 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.我试图在 signUpStarting() 函数中看到警报,但在那里我只得到了第四页值的响应,而其他值由于变量为空而没有显示任何内容。

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.我不知道如何在不使用 localStorage 或 cookie 并将所有数据发布到服务器的情况下始终保存变量值。而且我认为如果我知道将我网站的所有 html 页面编码为单个 JS 文件,这会更容易。

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.我不知道如何在不使用 localStorage 或 cookie 并将所有数据发布到服务器的情况下始终保存变量值。而且我认为如果我知道将我网站的所有 html 页面编码为单个 JS 文件,这会更容易。

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).您无法在 Web 浏览器环境中的页面加载之间将数据存储在内存中,因为当浏览器从页面导航到新页面时(即使它们在两个页面上使用相同的 javascript),所有 javascript 变量都会自然地销毁。 Thus, you have to save it somewhere with more permanence: localStorage, cookies, or on the server via POST or GET.因此,您必须将其保存在更持久的地方:localStorage、cookies,或者通过 POST 或 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.我推荐的是废弃四个不同的 html 页面,并简单地使用一个随着用户填写数据而动态变化的 html 页面。 This way the browser will not eliminate data before you are ready to POST it to the server.这样浏览器就不会在您准备好将数据发布到服务器之前消除数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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