简体   繁体   English

在另一个JavaScript文件中包含JavaScript文件

[英]Including JavaScript file in another JavaScript file

I have two JavaScript files I am using and I'd like to include one in another. 我有两个正在使用的JavaScript文件,我想一个包含另一个。

validate.js: validate.js:

function validateEmail(userEmail) {
    var email = userEmail;
    var emailFilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if (emailFilter.test(email)) {
    //alert('Please provide a valid email address');
        email.focus;
        return true;
    }
    else {
        return false;
    }
}

navigation.js navigation.js

$(document).ready(function() {
    var imported = document.createElement('script');
    imported.src = 'lib/validation.js';
    document.head.appendChild(imported);

    console.log("DOCUMENT IS READY!");

    var viewsWrapper = $("#views-wrapper");
    var loginButton = $("#login-button");
    var registerButton = $("#register-button");

    // Login Link
    // TODO: Unclear if needed
    $("ul li.login").click(function() {
        $.get('/login', function(data) {
            viewsWrapper.html(data);
        });
    });

    $('#usernamefield').blur(function() {
        var sEmail = $('#usernamefield').val();
        if ($.trim(sEmail).length == 0) {
            alert('Please enter valid email address');
            e.preventDefault();
        }
        if (validateEmail(sEmail)) {
            alert('Email is valid');
        }
        else {
            alert('Invalid Email Address');
            e.preventDefault();
        }
    });
});

(There is more code which is not relevant) (还有更多不相关的代码)

I've tried using solutions described on this page here 我已经使用这个网页上描述的解决方案,试图在这里

As shown above in navigate.js (commented area), but that is not working. 如上面的navigation.js(注释区域)所示,但这不起作用。
I am trying to call the validateEmail function which is located in validate.js from within navigation.js, but I can't seem to do that. 我正在尝试从navigation.js内调用validate.js中的validateEmail函数,但似乎无法做到这一点。 None of the solutions I've seen have helped. 我见过的解决方案都没有帮助。

Any help is appreciated. 任何帮助表示赞赏。

You can't include one JS file from another in any simple manner. 您不能以任何简单的方式包含另一个JS文件。 But you can include both from the page that uses them. 但是您可以在使用它们的页面中同时包含这两个元素。 Since your validateEmail function looks to be global, the other function will simply reference it. 由于您的validateEmail函数看起来是全局的,因此另一个函数将简单地引用它。

Alternately you can use one of the many module loaders that exist. 或者,您可以使用现有的许多模块加载器之一。 I would suggest that you save that for later, until you can do this simpler problem. 我建议您将其保存以备后用,直到您可以解决此简单问题为止。 But tools like Require.js and its kin will allow one module to specify its dependencies upon another and handle the loading of the dependencies. 但是,诸如Require.js及其同类之类的工具将允许一个模块指定其对另一个模块的依赖关系,并处理依赖关系的加载。

Or as @Jason suggested, you can also use a script combiner, which is commonly used to speed up sites by allowing you to develop with multiple files but deploy with single ones. 或按照@Jason的建议,您还可以使用脚本组合器,该组合器通常用于加速站点,方法是允许您使用多个文件进行开发,但可以使用单个文件进行部署。

But really, first you need to work on the basics, and that involves learning how scripts are used on web pages and how they talk to one another. 但是实际上,首先,您需要研究基础知识,这涉及到学习如何在网页上使用脚本以及它们如何相互通信。

What I would recommend is using one of the many JavaScript libraries such as YUI Compressor or Google Closure Compiler to combine all your javascript files into a single file. 我建议使用许多JavaScript库之一(例如YUI Compressor或Google Closure Compiler)将所有javascript文件合并为一个文件。 These libraries will also take care of minifying/compressing them as well. 这些库也将负责缩小/压缩它们。

Don't roll your own... 不要自己动手...

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

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