1.4.0 • Published 2 years ago

webtools-js v1.4.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

webtools-js

Just some JavaScript tools for websites to not bloat the code with larger frameworks.

Functionality

  • 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

Benefits

  • Vanilla, uncompiled JavaScript in ES 5.1 Syntax
  • No dependencies

Cookies

wt.Cookies.set(name, value, days)

Set a Cookie. If "days" is not set, a session cookie is written

// write a session cookie
wt.Cookies.set("testcookie", "123")

wt.Cookies.get(name)

Read a Cookie

wt.Cookies.remove(name)

Remove a Cookie

Observed

Observe variables

var observedVar = new wt.Observed(
    function(newValue, oldValue) { 
        // callback on change 
    })

Set values with

observedVar(value)

Get values with

var value = observedVar()

HttpRequest

wt.HttpRequest.get(url, onSuccess, onError)

wt.HttpRequest.get("test-request.txt", function (response) {
    // success
    console.log(response)
}, function (errorMessage) {
    // failure
    console.error(errorMessage)
})

Utils

wt.Utils.openExternalLinksBlank()

Opens all external links in a new tab, except when they have target="_self".

Examples

// 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()
1.4.0

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.0

3 years ago

1.0.9

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago