Reload script whenever the event happens

5

Hello. I was looking for and I do not find anything related here, so I'm coming to ask. I'm working with a plugin ' link ', and I want to give some functions to get better.

I've already done an image upload field, but this is not from submit, I'm using FileReader to load the image. What is happening, the script of this plugin runs in 'static' mode ie it comes into action as soon as the script is loaded, so I can not resize the photo that was rendered.

I wanted to know if there is such a way as to each 'change' event in the input it reloads the script, so it can get the information from the image.

    
asked by anonymous 02.04.2016 / 16:55

1 answer

0

Bruno, you can create a function to perform the search of the plugin asynchronously. This way, as soon as the field changes, it calls the load function.

function loadScript(scriptName, callback) {
    if (!jsArray[scriptName]) {
        jsArray[scriptName] = true;

        // Adiciona o script na head do documento
        var body    = document.getElementsByTagName('body')[0],
            script  = document.createElement('script');
        script.type = 'text/javascript';
        script.src  = scriptName;

        // então vincula o evento ao callback passado
        script.onload = callback;

        // dispara o carregamento
        body.appendChild(script);

        // Limpa a referência do DOM
        //body = null;
        //script = null;

    } else if (callback) {

        // Executa o Callback
        callback();
    }

}
    
02.04.2016 / 19:10