I'm developing an app that uses a certain site to make payments easier, and the way it handles payments requires to import some javascript from this url https://bridge.paymill.com/
that contains the script.
The fact is, I'm using require js to load all the scripts, in my main.js configuration, I'm trying to make it this way:
requirejs.config({
...
'paymill': 'https://bridge.paymill.com/',
...
});
But this, of course, tries to fetch from https://bridge.paymill.com/.js
, which is not the correct url (it's without the last .js)
How could I notify requirejs to load this without appending '.js' at the end?
Answer: Use the export and import Statement Since ECMAScript 6 (or ES6) you can use the export or import statement in a JavaScript file to export or import variables, functions, classes or any other entity to/from other JS files.
To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file.
Dynamic loadingThose files can be loaded asynchronously in JavaScript. To load a JavaScript file dynamically: Create a script element. Set the src , async , and type attributes.
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.
Put a question mark at the end of the URL
requirejs.config({
...
'paymill': 'https://bridge.paymill.com/?',
...
});
https://bridge.paymill.com/?.js
is a valid URL with .js
as a GET request.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With