JavaScript How can you capture the result of a HTML script tag in a variable?

I have been reading about AMD and implementations like requirejs. Most of the resources covers the usage and API's.

But, when it comes to implementing this, how can you load a JavaScript file into a variable just like that? For example, you can see functions like this:

define(['jquery'], function($){
});

var jquery = require('./jquery');

From an API consumer's perspective, all I can understand is that the file jquery.jshas magically become $, jQuery etc? How is that achieved?

Any examples would be helpful.

How do AMD loaders work under the hood? is a helpful read.

Edit: I think the eval answers below are nice because it actually is an eval problem in some ways. But I would like to know this from the perspective of an AMD specs implementation.

Answer:1

You don't load a javascript file into a variable, this is instead done by things such as browserify or webpack. Javascript itself can do this, but these modules generate a single file containing all your code. By calling require("file"), you are calling browserify's function to load a file named "file" stored in the code.

An example of this can be if you have a module

function demoModule(){
    console.log("It works!");
}
module.exports = demoModule

This makes module.exports now contain the "entire" contents of the file

Browserify/webpack puts that into a function that returns the module.exports of that file

function require(filename) {
    switch(filename){
        case "demofile":
            let module = {exports:{}}; ((module) => {
               function demoModule(){
                   console.log("It works!");
               }
               module.exports = demoModule
            })(module)
            return module.exports;
    }
};
    
require("demofile")();
Answer:2

You know how you can say eval(alert("hello!")) and it executes the code?

You can also:

var str = "hello!"
eval('alert("' + str + '");')

So the next step is to have a file that has your actual script in it:

var str = "hello"
alert(str)

then you can use a standard AJAX request to fetch that file into a variable, and you can eval() that variable.

Technically, eval() is considered evil - fraught with dangers - but there are other solutions (for example, injecting a script tag into the document body). I just went with eval() here to make the explanation easier.

Answer:3

i m trying to remove tr from table using closest function. this function is working properly out of the $.post request but when same script is used in post request its not working. My Code is $("....

i m trying to remove tr from table using closest function. this function is working properly out of the $.post request but when same script is used in post request its not working. My Code is $("....

  1. didn't
  2. https://www.disneyplus.com/
  3. didn't i
  4. didn't i lyrics
  5. disney plus
  6. didn't i onerepublic
  7. didnt even notice
  8. didn't ask
  9. disney store
  10. disney world
  11. disneyland
  12. didn't synonym
  13. didn't ask meme
  14. disneyland tickets
  15. disney +
  16. didn't know i was pregnant
  17. didn't detect another display
  18. didn't cha know
  19. disney cruise
  20. disney movies

I have an input field where I expect users to type text that contains 1 of many keywords that will trigger different audio files depending on the keyword. (I know that's not really smart from UX point ...

I have an input field where I expect users to type text that contains 1 of many keywords that will trigger different audio files depending on the keyword. (I know that's not really smart from UX point ...

I have written Backbone view - ListView and want to display table in to it. For individual table rows, there is another view - EmployeeView. I am binding click event on delete button which is inside ...

I have written Backbone view - ListView and want to display table in to it. For individual table rows, there is another view - EmployeeView. I am binding click event on delete button which is inside ...

  1. event in backbone view
  2. backbone event click outside view
  3. trigger event in backbone view

this is my code currently: let array = [`1 (1).jgp`,`1 (2).jgp`,`1 (3).jgp`,`1 (4).jgp`,`1 (5).jgp`,`1 (6).jgp`,`1 (7).jgp`,`1 (8).jgp`,`1 (9).jgp`,`1 (10).jgp`,`1 (11).jgp`] //rest of the code Is ...

this is my code currently: let array = [`1 (1).jgp`,`1 (2).jgp`,`1 (3).jgp`,`1 (4).jgp`,`1 (5).jgp`,`1 (6).jgp`,`1 (7).jgp`,`1 (8).jgp`,`1 (9).jgp`,`1 (10).jgp`,`1 (11).jgp`] //rest of the code Is ...