WebJS is a framework for buiding standard multipage web applications.
Step 1) Create a file config.js
var APP_ID = ""; // Your APP unique ID (optional)
var WEBSITE_URL = ""; // Your website root URL
var API_URL = ""; // Your website API URL (optional)
if (location.hostname === "localhost" || location.hostname === "127.0.0.1" || location.hostname === "") {
WEBSITE_URL = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port;
}
Step 2) Add to your webpage
<script src="config.js"></script>
<script src="https://cdn.jsdelivr.net/gh/lesichkovm/web@2.7.0/bin/web.js"></script>
Step 1) Create a file config.js
var APP_ID = ""; // Your APP unique ID
var WEBSITE_URL = ""; // Your website root URL
var API_URL = ""; // Your website API URL
if (location.hostname === "localhost" || location.hostname === "127.0.0.1" || location.hostname === "") {
WEBSITE_URL = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port;
}
Step 2) Download the web.js library locally and add to your webpage
<script src="config.js"></script>
<script src="web.js"></script>
Hint: Alternatively you may want to download automatically using Gulp
gulp.task('web', function (done) {
var url = "https://cdn.jsdelivr.net/gh/lesichkovm/web@latest/bin/web.js";
download(url)
.pipe(gulp.dest("js/"));
done();
});
Hint: Advanced optional. You may want to group config.js with web.js to reduce the number of the HTTP calls.
WebJS registers itself under the $$ namespace.
$(function(){
var name = $$.getUrlParam('name');
alert('Your name is ' + name);
});
Returns a key from registry or NULL if not set
Sets a key-value pair to registry or NULL if not set
Returns a hash parameter or NULL if not set
Returns the current URL
Returns a single query parameter or NULL if not set
var name = $$.getUrlParam("name");
alert("Hi" + (name==null ? "Stranger" : name));
Returns the query parameters from the current URL
Returns the language or 'en' if not set
Sets the language
Returns the authentication token or NULL
Sets the authentication token. To remove set it to NULL
Returns the authenticated user or NULL
Sets the authenticated user. To remove set it to NULL
Redirects to current webpage to the specified URL (relative or absolute)
Calls the API with the specified action and data. Returns a promise
var p = $$.ws('login',{username:"",password:""});
p.done(function(response){ }); // Call successful
p.fail(function(error){ console.log(error) }); // Call failed
p.always(function(){ }); // After call is completed
If the API_URL ends with / or .json . The actions will be send like this:
api.url.com/?command=action
Otherwise they will be send using nice URLs
api.url.com/action
Logs message to console, if debug is enabled
The registry provides persistence across requests using Local Storage.
Gets a key from the registry
Sets a key in the registry. Optional expiration time in seconds
Removes a key from the registry
Empties the registry