JavaScript function not defined after $ .getScript ('');

0

I need to load an .js file after an ajax call. I've been looking for and found $.getScript('') to call my file, it opens, everything works fine, but the functions defined in that file are not being found.

I have a clicklistener function defined like this:

var record = $('#record');

record.onclick = function () {

            startTimer(1);
            mediaRecorder.start(50);
            console.log("recorder started");

        }

When I click the button with the id record nothing happens. I already tried to put a name in this function and call it on the button:

<button type="button" onclick="javascript:startRecord();" id="record">Record</button>

but also not recognized. What is the correct technique to do what I want?

Note: I do not put the script on the page (before ajax call) because I need values and DOM elements that result from this call. I am working on MVC, the ajax call is in a razorView returns html resulting from a controller

    
asked by anonymous 27.11.2017 / 13:51

1 answer

0

It seems to me that you are not granting that the script has been loaded and / or forgetting to initialize something, if it is only the first case this should resolve:

  <button type="button" id="record">Record</button>

Bind in the event of $ .GetScript ('')

    $.getScript( "seu_script.js" )
    .done(function( script, textStatus ) 
     {
       $('#record').click(function(){
        mediaRecorder.start(50);
        console.log("recorder started");
       });
     });
    
28.11.2017 / 12:08