简体   繁体   English

在另一个包含的javascript中包含javascript

[英]including javascript in another included javascript

I am using the JQuery.UI datepicker, but have several changes that need to be applied that update the plug-in widget before it can actually be used in my web-app. 我正在使用JQuery.UI日期选择器,但是需要进行一些更改才能更新插件小部件,然后才能在Web应用程序中实际使用它。 For testing, I simply load JQuery and the datepicker libraries with the normal tags, then put in the modifying code in its own tag, and finally I put in the tag for the datepicker and the initializing code in yet another tag. 为了进行测试,我只需要使用常规标签加载JQuery和datepicker库,然后将修改后的代码放入其自己的标签中,最后,将datepicker的标签以及初始化代码放入另一个标签中。 This all works when there is only one page that uses the modified datepicker. 当只有一页使用修改后的日期选择器时,所有这些都有效。

Now I want to start using modified data on several pages and I don't want to have to maintain all of the modifications for the datepicker in each place where I use it. 现在,我想开始在多个页面上使用修改后的数据,而不必在每个使用日期的地方都保留对datepicker的所有修改。 What I'd like to do is create a wrapper file for the datepicker, which includes the modifying code and the includes needed to use datepicker, then in the pages that use a datepicker, I'd just include the wrapper script file. 我想做的是为datepicker创建一个包装文件,其中包括修改代码和使用datepicker所需的include,然后在使用datepicker的页面中,仅包含包装脚本文件。

This all boils down to including a javascript file in another included javascript file. 这全部归结为在另一个包含的javascript文件中包含javascript文件。

I realize that this is a duplicate of some of the other questions, but when I tried the suggested fixes in those other questions, such as the document.write, and the JQuery addScript techniques, they don't seem to work for me in Chrome. 我意识到这是其他一些问题的重复,但是当我尝试了其他问题(如document.write和JQuery addScript技术)中的建议修复程序时,它们似乎不适用于Chrome 。 The first problem I run into is that the addScript function tells me that there is a problem way down deep in the JQuery library, which is loaded normally with a tag in the main file. 我遇到的第一个问题是addScript函数告诉我在JQuery库中存在一个问题,该库通常在主文件中带有标记加载。 If I comment out the addScript then the error doesn't occur. 如果我注释掉addScript,则不会发生错误。 The document.write method seems to run, but then none of the datepickers work. document.write方法似乎正在运行,但随后所有的datepickers都不起作用。

I'd like to know how the best do the script load within another script load, so I can create my wrapper, and the solution needs to work in all of the major browsers. 我想知道在另一个脚本加载中如何最好地加载该脚本,因此我可以创建包装器,并且该解决方案需要在所有主流浏览器中都能正常工作。

The wrinkle in all of this is that the process must be adaptable to pages created using PHP + Ajax, but I first want to get a pure HTML + Javascript + JQuery + JQuery.UI Datepicker solution working. 所有这一切的麻烦之处在于,该过程必须适应使用PHP + Ajax创建的页面,但是我首先要获得一个纯HTML + Javascript + JQuery + JQuery.UI Datepicker解决方案。

If this has already been answered, and works with the constraints that I've outlined here, please point me to that solution, please don't just say 'duplicate question' and leave it at that. 如果已经回答了这个问题,并且可以解决我在此处概述的限制,请向我指出该解决方案,请不要只说“重复问题”,而应该保留它。 I've reviewed the solutions in this forum and others, and have seen lots of simple and more complex solutions that just don't work or result in out right errors. 我已经在该论坛和其他论坛上回顾了这些解决方案,并且看到了许多简单而又复杂的解决方案,这些解决方案无法正常工作或导致正确的错误。

It would be really, really, nice if tag did support nested includes, this all would be much easier, and using this concept to support wrappers to isolate overrides to the base library objects is what objects are all about. 如果标记确实支持嵌套包含,那将非常非常地好,这一切都将变得更加容易,并且使用此概念来支持包装器以隔离对基本库对象的替代就是对象的全部目的。 So a robust, cross browser technique that supports the and css includes really is over-due. 因此,支持和CSS的健壮的跨浏览器技术确实过期了。 If it's out there then I missed it, but from what I've seen it's not there in a 'ready-for-prime-time' fashion. 如果它在那里,那么我会想念它的,但是从我所看到的来看,它并不是以“准备好黄金时段”的方式出现的。

Thanks, Howard Brown 谢谢,霍华德·布朗

have you tried something like this? 你尝试过这样的事情吗?

var Loader = function () { }
Loader.prototype = {
    require: function (scripts, callback) {
        this.loadCount      = 0;
        this.totalRequired  = scripts.length;
        this.callback       = callback;

        for (var i = 0; i < scripts.length; i++) {
            this.writeScript(scripts[i]);
        }
    },
    loaded: function (evt) {
        this.loadCount++;

        if (this.loadCount == this.totalRequired && typeof this.callback == 'function') this.callback.call();
    },
    writeScript: function (src) {
        var self = this;
        var s = document.createElement('script');
        s.type = "text/javascript";
        s.async = true;
        s.src = src;
        s.addEventListener('load', function (e) { self.loaded(e); }, false);
        var head = document.getElementsByTagName('head')[0];
        head.appendChild(s);
    }
}

usage 用法

var l = new Loader();
l.require([
    "example-script-1.js",
    "example-script-2.js"], 
    function() {
        // Callback
        console.log('All Scripts Loaded');
    });

You could potentially nest as many includes as you want (each one in it's respective callback) and they would all still work. 您可以根据需要嵌套尽可能多的包含项(每个都包含在各自的回调中),它们仍然可以使用。

Credit to CSS Tricks for the solution 感谢CSS Tricks提供解决方案

I highly doubt ur gonna like my suggestion but ima give it a go anyways 我非常怀疑您会喜欢我的建议,但是ima无论如何都会尝试

create a file called includes.php 创建一个名为includes.php的文件

put all the stuff in tht file 将所有内容放入文件

use 采用

<? include("includes.php"); ?>

where needed 需要的地方

this way you can put all your scripts in one file and have them included in as many pages as u want with one simple line of code 这样,您可以将所有脚本放在一个文件中,并用一行简单的代码将它们包含在您想要的页面中

You could use a git system local repository with your changes so you can do some merging when needed. 您可以在更改时使用git系统本地存储库,以便在需要时进行一些合并。 That would be what i'd suggest. 那就是我的建议。 Fork the original branch and then add your changes. 分叉原始分支,然后添加您的更改。 This is also known as a patch file. 这也称为补丁文件。

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

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