1

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. 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. 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. 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.

This all boils down to including a javascript file in another included javascript file.

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. 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. If I comment out the addScript then the error doesn't occur. The document.write method seems to run, but then none of the datepickers work.

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.

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. 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

  • Why not copying it into one file? – Jonas Wilms Jul 22 '16 at 05:05
  • Why do you need to load it later? – Jonas Wilms Jul 22 '16 at 05:05
  • what do you mean by **including a javascript file in another included javascript file** – madalinivascu Jul 22 '16 at 05:06
  • You refer to attempts using solutions from other questions. Please show the actual code you've tried, provide links to those questions, and describe why it didn't work (behaviour, error messages in the console, and so on). – GolezTrol Jul 22 '16 at 05:07
  • from what i can tell you want another js file with the custom datepiker code what is so hard to do – madalinivascu Jul 22 '16 at 05:24
  • Wow, this many responses in only 5 hours! –  Jul 22 '16 at 10:22
  • Jonas w - I'm trying to eliminate needing to go to each file/page where the datepicker is used to maintain it over time - Modularity. I don't need to delay loading. I want to wrap an 'include' file needed to use datepicker together with other related code. So in the page where the datepicker is used, there is a normal –  Jul 22 '16 at 10:36
  • Golez Trol - I would have and may still, but with these all of these responses I'd like to try out the many unique solutions that people have provided. –  Jul 22 '16 at 10:38
  • Madalin Ivascu - I detail how I see the included would be setup in my reply to Jonas w. I don't want all of the included to be made in each page that uses the datepicker. That leads to many pages needing to be updated when ever a new datepicker or patch modification is needed. I only want to go into the wrapper script that is included in each page and change the code there. What's so hard is I can't do nested –  Jul 22 '16 at 10:49

3 Answers3

3

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

sgarcia.dev
  • 5,671
  • 14
  • 46
  • 80
  • while this is perfectly viable and actually quite genius this would be far more tedious then the already suggested solutions –  Jul 22 '16 at 06:15
  • This truly is a unique solution! I see your credit to CSS Tricks so it appears that this might also allow me to load the two CSS files that I need in each page that uses datepicker. Interesting. As to other methods of doing the same. The ones I refer to initially didn't work, either the datepicker didn't function at all or script errors were reported. –  Jul 22 '16 at 10:55
  • Continuing - Of the new suggestions, I've already commented on them, above, but so far, except for your's they don't meet my need or would be messy to maintain in the case of combining the code into one –  Jul 22 '16 at 11:04
0

I highly doubt ur gonna like my suggestion but ima give it a go anyways

create a file called 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

  • It would be even more efficient to do this once and cache the flattened and optionally minimized file. – GolezTrol Jul 22 '16 at 05:16
  • why the downvote lol this is a perfectly valid answer not the most optimal maybe but still valid in practice ive seen various websites,engines,frameworks...using this method –  Jul 22 '16 at 05:19
  • Joseph Flames - I really like this one the best, and wouldn't vote it down. Instead, I would vote it up, but I don't have the rep to do so. Its simple and does exactly what I'm trying to do! Since I am using PHP, I'm going to implement this. Your other solution may still come in handy in Non-PHP cases, so I still need to try it out. :) –  Jul 22 '16 at 11:24
  • Golez Troi - You say why not cache and flatten and minimize the file - I'm not clear on how to do all of this in PHP. I could globally include the 'datepicker_includes.php' file in one header that is used by all of the other pages, but then there could be a lot of overhead for the majority of the pages that don't use datepicker. Since I don't need it everywhere, only 5 or 6, then the overhead of only having these few pages should be less than having it in all pages. At least that's the way I see it. I'm new to PHP, so I could be all wrong about this. –  Jul 22 '16 at 11:32
  • Continued - On to flatten the file. If flattening means putting the library and patch code in one file, then no, To me, the library should be separate from the patch code - so I can swap-out the library for newer versions. Having the naked library code, right out there,is difficult, it's not formatted, too long, and distracting, and below the library code are 5 patches, maybe more later. A comment before the datepicker library code 'chunk', saying 'goto line xxxx and DON'T EVEN THINK ABOUT EDITING THIS THING!' would help, but keeping the library in its original include file is cleaner. –  Jul 22 '16 at 11:57
  • Continued - Minimizing the file. If this mean removing code from the datepicker library and the patches, then I'm not up to that. Sure, the datepicker library has a lot more things in it than I using or need. Perhaps too much, but the patches are in total very short and don't have extra code-load. However, I may want some of the extra features, later, and untangling the inter-dependencies of the library code is way too much for me to try. It would be better for me just to write my own datepicker,but in this leads to a not inconsiderable undertaking for which I just don't have the time. –  Jul 22 '16 at 12:08
  • Again, thanks to all of you - lots of great comments and solutions - very helpful. –  Jul 22 '16 at 12:10
  • Joseph Flames - I will be your suggested PHP include method, but for my own interest, I followed your link back to CSS Tricks and found the following, which looked very favorable: (Sorry can't get this code to format properly) - $.getScript( 'jquery_ui_datepicker_1.12.0.js' ) .done( function( script, textStatus ) { alert( 'Script Length: ' + script.length + '\r\n' + 'Status: ' + textStatus ); } ) .fail( function( jqxhr, settings, exception ) { alert( 'Triggered ajaxError handler.' ); } ); –  Jul 22 '16 at 13:57
  • Continued - The $.getScript outputs the following error to the console: jquery-1.12.4.js:10254 XMLHttpRequest cannot load file:///D:/Users/.../Datepicker Widget.js?_=1469196074026. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.send @ jquery-1.12.4.js:10254 Datepicker Widget.html:118 Uncaught TypeError: $(...).datepicker is not a function. Do you or anyone else know how to get past this protocol issue without using a HTTP or HTTPS protocol prefix and a Domain URL? I'm running local right now. THX! –  Jul 22 '16 at 14:07
  • im not sure abt tht but if my answer helped you please hit that checkmark :) –  Jul 22 '16 at 19:44
-1

You could use a git system local repository with your changes so you can do some merging when needed. That would be what i'd suggest. Fork the original branch and then add your changes. This is also known as a patch file.

JQluv
  • 244
  • 1
  • 6
  • That is hard to maintain, and things might break easily when you want to update a library. – GolezTrol Jul 22 '16 at 05:15
  • Why would I get downvoted for this? Its a valid solution a real solution may not be agree'd for you but its a solution and what most people actually would do. or they would submit the changes they want to the original creator. – JQluv Jul 22 '16 at 05:26
  • JQluv - Thanks for chiming in. I wouldn't have voted it down as its a very good way to maintain the code base. However, I think this is the same as putting all of the datepicker code together in the wrapper script file - Easy to do, less clear to manage. I think pouring the +1000 lines of datepicker's library code into the same file with all of the patch code really would make understanding the whole thing difficult and maintain. Keeping the library separated from the patches helps to keep it all much clearer and modular. Having different files for each of the patches may be over kill. –  Jul 22 '16 at 11:18