I have a component that displays the card payment option.
I am trying to load the conekta script only when it is needed. When the component that occupies the connect script is rendered, in the created hook I load the script.
created() {
if (document.getElementById("conekta")) return; // was already loaded
var scriptTag = document.createElement("script");
scriptTag.src = "https://cdn.conekta.io/js/latest/conekta.js";
scriptTag.id = "conekta";
document.getElementsByTagName("head")[0].appendChild(scriptTag);
},
What I don't like about this is that it generates errors that the Conekta object does not exist
I thought about downloading the cdn and uploading it as a static file using functions like require or import but this would cause me problems if conekta updated their cdn script.
also try to listen to the ready or load event of the script but it doesn't work either
function loadScript() {
if (document.getElementById("conekta")) return; // was already loaded
var scriptTag = document.createElement("script");
scriptTag.src = "https://cdn.conekta.io/js/latest/conekta.js";
scriptTag.id = "conekta";
document.getElementsByTagName("head")[0].appendChild(scriptTag);
document.getElementById("conekta").addEventListener("load", function(){
component = { my component };
});
export default component;
}
loadScript();