Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I load a script dynamically via Backbone? [duplicate]

Inside one of my views, I'd like to load this script:

<script type="text/javascript" src="https://domain.com" id="hello" name="abc"></script>

Is it possible to do that?

like image 745
TIMEX Avatar asked Jul 16 '14 11:07

TIMEX


People also ask

How do you load third party scripts dynamically in react?

We start by creating an empty <script></script> tag in the memory as script and then assign the necessary attributes to its src and the id to identify the script later. Finally, we append the script to our <body></body> tag to actually load this.

How do I load a JavaScript script?

For loading a script file dynamically using JavaScript, the basic steps are: Create the script element. Set the src attribute on the script element to point to the file we want to load. Add the script element to the DOM.


1 Answers

  1. Manually appending it:

    $('head').append('<script type="text/javascript" src="https://domain.com" id="hello" name="abc"></script>')
    
  2. Using $.getScript:

    $.getScript('https://domain.com').done(function () {
        // loaded!
    });
    
  3. RequireJS:

    require.config({
        paths: {
            "myscript": "https://domain.com"
        }
    });
    
    require(['myscript'], function () {
        // loaded!
    });
    
like image 69
jgillich Avatar answered Oct 29 '22 15:10

jgillich