Ajax link system for modern website. Convert any link in your page to ajax link.
npm install @kiralt/bjaxAjax link system for modern website. Convert any link in your page to ajax link.
New V2 version on pure VanilaJS, lightweight and super fast!
``shell`
npm install @kiralt/bjax
`html`
In browser build, all funtions will be exported on window.Bjax.
You can convert all links to Bjax links using bindLinks with a selector.
`js`
bindLinks('a')
Or you can bind on data-bjax selector.
`js`
bindLinks('[data-bjax]')
You can specify source and target using data attributes:
`html`
My link
Or you can specify that information when binding:
`js`
bindLinks('data-bjax', {
source: '#source',
target: '#target'
})
Also, you can use data-selector attribute or selector parameter to specify both target & source with one parameter.
You can load link manually using loadLink function.
`js`
loadLink('https://my.page/second-page', {
target: '#part'
})
This will download whole page and insert it into element with part ID.
Using liveBind and loadLink you can customize bindings as much as you wish.
`js`
liveBind('[data-bjax]', 'click', function(event, element) {
event.preventDefault()
if (element instanceof HTMLAnchorElement) {
loadLink(element.href, {
selector: '#container'
})
}
})
Downloads page from url and updates current page using downloaded HTML.
`js`
loadLink('https://my.page/second-page')
Options:
* target - DOM selector where should be inserted downloaded page HTML (default is body).body
* source - DOM selected which indicates which part from downloaded page should be extracted (default is ).target
* selector - Overwrites both and source (default: undefined).url
* shouldUpdateUrl - indicates if the url should be updated using parameter using pushState (default is true).{}
* loader - custom loader instance (default is empty loader - ).
* successCallback - callback which is called if the function finished the work without an error.
* errorCallback - Callback which is called if the function had an error and failed to finish.
Binds loadLink funtion on given selector using liveBind.
`js`
bindLinks('a[data-bjax]')
Supports all loadLink options.
Additional options:
* url - target page URL, default is undefined and urlAttribute parameter is used.url
* urlAttribute - parameter value, which is taken from clicked element (default is href).target
* targetAttribute - parameter value, which is taken from clicked element (default is data-target).source
* sourceAttribute - parameter value, which is taken from clicked element (default is data-source).selector
* selectorAttribute - parameter value, which is taken from clicked element (default is data-selector).
Listens eventName events on window object. Because it binds on window` object, you can change inner DOM and it won't effect this bind at all.