Utilities
Helper methods to make working with the portal a bit easier.
serialize() |
---|
Serializes all of the data in a form into an object. |
Syntax
/** * @param {Node} form The form to serialize * @param {Boolean} rawHTML If true, do not strip HTML from the values * @return {Object} The serialized form data */ portal.serialize(form, rawHTML) |
Example
// Get a form var form = document.querySelector('form'); // Get all of the data in a form var formData = portal.serialize(form); // Get all of the form data with HTML allowed var formHTML = portal.serialize(form, true); |
buildQuery() |
---|
Builds a query string from an object of key/value pairs. |
Syntax
/** * @param {Object} data The data to turn into a query string * @return {String} The query string */ portal.buildQuery(data) |
Example
// An object of key value pairs var data = { name: 'Dee Developer', favoritePet: 'Dog' }; // Build a query string // Returns "name=Dee%20Developer&favoritePet=Dog" var query = portal.buildQuery(data); |
getParams() |
---|
Gets the query string parameters from a URL as an object of key/value pairs. |
Syntax
/** * @param {String} url The URL [optional, defaults to window.location] * @return {Object} The URL parameters */ portal.getParams(url) |
Example
// Get the query string parameters from a URL var url = 'https://myportal.com?name=Dee%20Developer&favoritePet=Dog'; var params = portal.getParams(url); // Returns "Dee Developer" params.name; // Returns "Dog" params.favoritePet; |
loadJS() |
---|
Loads an external JavaScript file into the DOM. |
Syntax
/** * @param {String} src The file to load * @param {Function} cb A callback to run after the file loads [optional] * @param {Boolean} ordered If true, do NOT load async [optional] */ portal.loadJS(src, cb, ordered) |
Example
// Load a JS file portal.loadJS('/path/to/my/scripts.js'); // Load a JS file and run a callback after it loads portal.loadJS('/path/to/my/scripts.js', function () { myScripts.init(); }); // Load multiple files and preserve the source order portal.loadJS('/path/to/my/script1.js', true); portal.loadJS('/path/to/my/script2.js', true); portal.loadJS('/path/to/my/script3.js', true); portal.loadJS('/path/to/my/script4.js', true); // Load multiple files and preserve the source order // Run a callback after the last on eloads portal.loadJS('/path/to/my/script1.js', true); portal.loadJS('/path/to/my/script2.js', true); portal.loadJS('/path/to/my/script3.js', true); portal.loadJS('/path/to/my/script4.js', function () { myScripts.init(); },true); |
loadHead() |
---|
Add an element into the head element. |
Syntax
/** * Inject HTML elements into the <head> * @param {String} type The HTML element type * @param {Object} atts The attributes and values for the element */ portal.loadHead(type, atts) |
Example
// Add a viewport element // <meta name="viewport" content="width=device-width, initial-scale=1"> portal.loadHead('meta', [ name: 'viewport', content: 'width=device-width, initial-scale=1' ]); |
html() |
---|
Sanitizes and renders an HTML string into the DOM. |
Syntax
/** * @param {String|Node} selector The selector or node to render into * @param {String} html The HTML to render * @param {Boolean} append If true, append to DOM instead of wiping existing out * @return {Array} An array of injected elements */ portal.html(selector, html, append) |
Example
// Render <h1>Hello world</h1> into the #app element portal.html('#app', '<h1>Hello world</h1>'); // Add <p>How are you today?</p> in the #app element after any existing content portal.html('#app', '<p>How are you today?</p>', true); |
Copyright © 2022. Cloud Software Group, Inc. All Rights Reserved.