webtools-js
Version:
Small vanilla webtools in small vanilla es5.
113 lines (83 loc) • 2.2 kB
Markdown
Just some `JavaScript` tools for websites to not bloat the code with
larger frameworks.
- Read and write **Cookies**
- **Observe** variables to call a callback on change
- Make **http requests**
- **Delegate** events
- Use a `documentReady` event handler
- Automatically **open external links in a new tab**
- Vanilla, uncompiled JavaScript in ES 5.1 Syntax
- No dependencies
Set a Cookie. If "days" is not set, a session cookie is written
```js
// write a session cookie
wt.Cookies.set("testcookie", "123")
```
Read a Cookie
Remove a Cookie
Observe variables
```js
var observedVar = new wt.Observed(
function(newValue, oldValue) {
// callback on change
})
```
Set values with
```js
observedVar(value)
```
Get values with
```js
var value = observedVar()
```
```js
wt.HttpRequest.get("test-request.txt", function (response) {
// success
console.log(response)
}, function (errorMessage) {
// failure
console.error(errorMessage)
})
```
Opens all external links in a new tab, except when they have `target="_self"`.
```js
// Cookies
// write a cookie
wt.Cookies.set("testcookie", "123")
// read a cookie
console.log("Cookie", wt.Cookies.get("testcookie"))
// Observed variable
// observe the value of the variable `observedVar`
var observedVar = new wt.Observed(function (newValue, oldValue) {
console.log("newValue", newValue)
console.log("oldValue", oldValue)
})
// write the value of the variable `observedVar`
observedVar("New Value")
// read the value of the variable `observedVar`
console.log("observedVar()", observedVar())
// HttpRequest
wt.HttpRequest.get("test-request.txt", function (response) {
// success
console.log(response)
}, function (errorMessage) {
// failure
console.error(errorMessage)
})
// Utils
// open all external links in a new tab, except when they have `target="_self"`
wt.Utils.openExternalLinksBlank()
```