Skip to content

Instantly share code, notes, and snippets.

@bulentsakarya
Created June 18, 2023 10:24
Show Gist options
  • Save bulentsakarya/a698289ddcefbf15fc1f8de7db014261 to your computer and use it in GitHub Desktop.
Save bulentsakarya/a698289ddcefbf15fc1f8de7db014261 to your computer and use it in GitHub Desktop.
Javascript Kodları
https://www.educative.io/answers/how-to-dynamically-load-a-js-file-in-javascript
function loadJS(FILE_URL, async = true) {
let scriptEle = document.createElement("script");
scriptEle.setAttribute("src", FILE_URL);
scriptEle.setAttribute("type", "text/javascript");
scriptEle.setAttribute("async", async);
document.body.appendChild(scriptEle);
// success event
scriptEle.addEventListener("load", () => {
console.log("File loaded")
});
// error event
scriptEle.addEventListener("error", (ev) => {
console.log("Error on loading file", ev);
});
}
loadJS("file1_path", true);
// If we set async false, file2 is loaded and executed first, then file3 will be loaded
loadJS("file2_path", false);
loadJS("file3_path", true);
Using Promise to load the script file
const loadScript = (FILE_URL, async = true, type = "text/javascript") => {
return new Promise((resolve, reject) => {
try {
const scriptEle = document.createElement("script");
scriptEle.type = type;
scriptEle.async = async;
scriptEle.src =FILE_URL;
scriptEle.addEventListener("load", (ev) => {
resolve({ status: true });
});
scriptEle.addEventListener("error", (ev) => {
reject({
status: false,
message: `Failed to load the script ${FILE_URL}`
});
});
document.body.appendChild(scriptEle);
} catch (error) {
reject(error);
}
});
};
loadScript("file1_url")
.then( data => {
console.log("Script loaded successfully", data);
})
.catch( err => {
console.error(err);
});
https://stackoverflow.com/questions/9422974/createelement-with-id
function createElement(element, attribute, inner) {
if (typeof(element) === "undefined") {
return false;
}
if (typeof(inner) === "undefined") {
inner = "";
}
var el = document.createElement(element);
if (typeof(attribute) === 'object') {
for (var key in attribute) {
el.setAttribute(key, attribute[key]);
}
}
if (!Array.isArray(inner)) {
inner = [inner];
}
for (var k = 0; k < inner.length; k++) {
if (inner[k].tagName) {
el.appendChild(inner[k]);
} else {
el.appendChild(document.createTextNode(inner[k]));
}
}
return el;
}
//examples
createElement("div");
will return this:
<div></div>
createElement("a",{"href":"http://google.com","style":"color:#FFF;background:#333;"},"google");`
will return this:
<a href="http://google.com" style="color:#FFF;background:#333;">google</a>
var google = createElement("a",{"href":"http://google.com"},"google"),
youtube = createElement("a",{"href":"http://youtube.com"},"youtube"),
facebook = createElement("a",{"href":"http://facebook.com"},"facebook"),
links_conteiner = createElement("div",{"id":"links"},[google,youtube,facebook]);
will return this:
<div id="links">
<a href="http://google.com">google</a>
<a href="http://youtube.com">youtube</a>
<a href="http://facebook.com">facebook</a>
</div>
You can create new elements and set attribute(s) and append child(s)
createElement("tag",{attr:val,attr:val},[element1,"some text",element2,element3,"or some text again :)"]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment