0

I am working on MVC appliction .I have a masterpage which is calling external javascript file and sometimes child page also calling the javascript file. i want if one time it is added by child page or masterpage it should not again add that Javascript file. please help me this i have done but is not working enter code here

    $(document).ready(function () {
        var fileref1 = document.createElement('script')
        fileref1.type = "text/javascript";
        fileref1.src = "~/Scripts/testZoomPlugin.js";
        fileref1.Id = "testZoomJS";
        if (typeof fileref1 == "undefined") {
            $.getScript('~/Scripts/testZoomPlugin.js');
        }
        fileref1.src = "~/Scripts/jquery.ui.widget.min.js";
        fileref1.Id = "testWidget";

        if (typeof fileref1 == "undefined") {
            $.getScript('~/Scripts/jquery.ui.widget.min.js');
        }
        fileref1.src = "~/Scripts/jquery.ui.slider.min.js";
        fileref1.Id = "testSlider";
        if (typeof fileref1 == "undefined"){
            $.getScript('~/Scripts/jquery.ui.slider.min.js');
        }
        if(typeof fileref1=="undefined"){
            $.getScript('~/Scripts/jquery.ui.slider.min.js');
    });

4 Answers 4

2

Just use RequireJS, it does what you need and have a straight-forward syntax, in your case it will be something like this :

<script src="~/Scripts/require.js"></script>
<script>
var modules = [], prefix = "~/Scripts/";
modules.push( prefix + "testZoomPlugin.js" );
modules.push( prefix + "jquery.ui.widget.min.js" );
    require( modules, function( someModule ) {
        //This function will be called when all the dependencies
        //listed above are loaded. Note that this function could
        //be called before the page is loaded.
        //This callback is optional.
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

Why would you not just get these files in your master page? they will then be accessible to your child pages.

I'm not sure there is a lot of value in doing all this checking, if the file has been downloaded once it will not be downloaded again on a child page as it will exist in cache.

Or am I missing something here?

6 Comments

The problem is not the downloading. Its also adding a new script tag and executing the module twice. Modules are also important if you are making a big app (managing dependencies by hand can be a PITA)
Surely though the master page in this situation will handle the script requirements in the same way as it would for say, the css?
Not necessarily. Dojo, the toolkit I use, allows you to have the dynamic script tags for rapid development and you can use an automatic tool later to mash them all together in a single file for production. Really neat...
We use that logic with Ext.js. Once it's deployed and the single file has been created it only needs to be included once, so I'm still not getting why you'd want to dynamically load/unload then have to potentially reload things, if they are all being mashed together anyway?
Its to avoid the separate compilation step during development. It also allows me to pull extra modules on demand in the console when im debugging.
|
0

I think you may be going about this backwards. In situations like this, you'd usually want to have the child page check for a certain variable being set to see if it should or shouldn't load a javascript file. For example: you could have a child page's ready function look like this:

$(function() {
  if( typeof $.fn.slider == 'undefined' )
    $('body').append('<script type="text/javascript" src="~/Scripts/jquery.ui.slider.min.js"></script>');
});

or something like that.

Comments

0

Your module-loading functions should cache all module names to prevent them being calledd twice. If your current functions doesn't do that already, make a wrapper that does.

var modules_loaded = {};
function caching_get_script(name){
    if(modules_loaded[name]) return;
    modules_loaded[name] = true;
    $.getScript(name)
}

Or something like that...

1 Comment

I just gave you an example <_<

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.