Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 19, 2023 08:58
Show Gist options
  • Save code-boxx/f8a97a8c2a1dafb8e0521772490bce80 to your computer and use it in GitHub Desktop.
Save code-boxx/f8a97a8c2a1dafb8e0521772490bce80 to your computer and use it in GitHub Desktop.
Reusable Javascript Components

REUSABLE JAVASCRIPT COMPONENTS

https://code-boxx.com/simple-reusable-javascript-components/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

var mylib = {
// (A) AJAX : AJAX FETCH
// url : target url
// done : function to call when server responds
ajax : (url, done) => {
fetch(url, { method:"POST" })
.then(res => res.text())
.then(txt => done(txt));
},
// (B) LOAD : USE AJAX TO LOAD CONTENTS
// url : target url
// target : id of target html element
load : (url, target) => mylib.ajax(
url, txt => document.getElementById(target).innerHTML = txt
)
};
<!DOCTYPE html>
<html>
<head>
<title>Simple Reusable Component Demo</title>
<!-- (A) LOAD LIBRARY -->
<script src="1a-mylib.js"></script>
</head>
<body>
<!-- (B) AJAX LOAD CONTENT -->
<div id="demo"></div>
<script>
mylib.load("1c-dummy.html", "demo");
</script>
</body>
</html>
<strong>It Works!</strong>
class Mylib {
// (A) AJAX : AJAX FETCH
// url : target url
// done : function to call when server responds
ajax (url, done) {
fetch(url, { method:"POST" })
.then(res => res.text())
.then(txt => done(txt));
}
// (B) LOAD : USE AJAX TO LOAD CONTENTS
// url : target url
// target : id of target html element
load (url, target) {
this.ajax(url, txt => document.getElementById(target).innerHTML = txt);
}
}
<!DOCTYPE html>
<html>
<head>
<title>Simple Reusable Component Demo</title>
<!-- (A) LOAD LIBRARY -->
<script src="2a-mylib.js"></script>
</head>
<body>
<!-- (B) AJAX LOAD CONTENT -->
<div id="demo"></div>
<script>
var mylib = new Mylib();
mylib.load("1c-dummy.html", "demo");
</script>
</body>
</html>
(() => {
// (A) AJAX : AJAX FETCH
// url : target url
// done : function to call when server responds
var ajax = (url, done) => {
fetch(url, { method:"POST" })
.then(res => res.text())
.then(txt => done(txt));
};
// (B) LOAD : USE AJAX TO LOAD CONTENTS
// url : target url
// target : id of target html element
var load = (url, target) => {
ajax(url, txt => document.getElementById(target).innerHTML = txt);
};
// (C) "PUBLIC FUNCTIONS"
window.mylib = { load : load };
})();
<!DOCTYPE html>
<html>
<head>
<title>Simple Reusable Component Demo</title>
<!-- (A) LOAD LIBRARY -->
<script src="3a-mylib.js"></script>
</head>
<body>
<!-- (B) AJAX LOAD CONTENT -->
<div id="demo"></div>
<script>
mylib.load("1c-dummy.html", "demo");
</script>
</body>
</html>
export function first () { alert("first!") }
export function second () { alert("second!") }
<!DOCTYPE html>
<html>
<head>
<title>Module Demo</title>
<script type="module">
import * as mylib from "./4a-mylib.js";
mylib.first();
mylib.second();
</script>
</head>
<body></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment