What is JSLib?
JSLib is a new library of methods which aim to help you write beautiful and more functional code.
How to start using JSLib
JSLib is just another JavaScript file so it can be imported to your web pages via the HTML script tag, like so:
<script src="jslib.min.js"></script>
For now, only the minified version of JSLib is available for use. The original, uncompressed version is being updated with comments so you can understand what is going on at each line.
Meet the functions/methods
-
load
Theloadmethod is a wrapper for theonloadevent handler, to be used to run code whilst the current web page is being loaded to the user's web browser, i.e. images and files are being downloaded from a server somewhere.Example
document.load(function() { console.log("I'm being loaded!"); });
-
ready
If you're familiar with the jQuery JavaScript library, you'd know when or how to use thereadyevent handler. You would use thereadymethod in JSLib in (almost) exactly the same way. You should use this method if you're writing JavaScript code that's going to make use of HTML elements within the HTMLheadtag, like so:Example
document.ready(function() { console.log("I'm ready"); });
-
press
Thepressmethod is a wrapper around JavaScript'sonclickevent handler, which is run whenever a targeted element is clicked with a mouse (or tapped on a touchscreen device). Here's how you might use this method:Example
document.ready(function() { var body = document.search("body"); body.press(function() { alert("I'm clicked!"); // You'd be better off not alerting everytime the page is clicked. }); });
-
html
Thehtmlmethod will retrieve and return theinnerHTMLvalue of the linked element. That's pretty much it. Here's how you may go about using this method:Example
document.ready(function() { var h1 = document.search("h1"); console.log(h1.html()); });
-
addCSS
TheaddCSSmethod quite literally serves to do what it's called: to add CSS, i.e. to the linked element. The method accepts one parameter which is an object. In jQuery, for example, you provide an object to thecssmethod to supply multiple properties to the linked object. Here's how you may use this method at its basic form:Example
document.ready(function() { var body = document.search("body"); body.addCSS({ background: "red", padding: "5px 10px" }); });
When the page has been loaded, in this case, the background colour of it will be red and it will have its padding set to
5pxtop and bottom, and10pxleft and right. However, this method has been built to be more functional that what it seems.Meet pipe nesting
A great feature of many CSS preprocessors (such as Sass) is nesting sub-properties in one block to avoid repetition, thus enforcing the rule of DRY (don't repeat yourself). Here's an example:
font: { family: sans-serif; size: 20px; }
The generated CSS file will append
fontbefore each sub-property name, which is valid CSS. TheaddCSSmethod also provides this cool functionality in its own way. Here's a demonstration, continuing on from the previous example:document.ready(function() { var body = document.search("body"); body.addCSS({ background: "red", padding: "5px 10px", "font: family | size | weight":"sans-serif | 20px | lighter" }); });
Once the webpage loads,
fontwill be appended to each sub-property name, in this case:familybecomesfont-family,sizebecomesfont-sizeandweightbecomesfont-weight. This is the syntax:property: sub-property-one | sub-property-two | etc... -
toggle
This method toggles the display of the linked element. If the property is shown on the page, it'll be hidden, and vice versa. Here's how you may go about using this method.document.ready(function() { var toggle_button = document.search("#toggle-button"); var elem_to_toggle = document.search("#elem-to-toggle"); toggle_button.press(function() { elem_to_toggle.toggle(); }); });
-
hover
Thehovermethod replicates theonmouseoverevent handler, which lets you perform certain tasks when the mouse is over the targeted element. Here's how you may use this method:document.ready(function() { var body = document.search("body"); body.hover(function() { console.log("The user has moved their mouse over the page."); }); });
-
leave
Theleavemethod replicates theonmouseleaveevent handler of JavaScript, which lets you perform certain tasks when the mouse is moved away from the targeted element. Here's how you may use this method:document.ready(function() { var body = document.search("body"); body.leave(function() { console.log("The user has moved their mouse away from the page."); }); });
-
setAttr
As you may have guessed, thesetAttrmethod of JSLib sets an attribute to the linked element. In its basic form, it replicates the JavaScriptsetAttributemethod. However, thesetAttrmethod is actually more powerful than that. It lets you provide any number of attributes and write it in the way you'd write it in HTML, through the use of the=symbol, like this:document.ready(function() { var paragraph = document.search("p"); // Let's imagine there's only one <p> tag on the page paragraph.press(function() { this.setAttr("title=You've hovered over me!|class=hover|disabled=false"); }); });
Once the mouse clicks on the paragraph element, the function will assign the three attributes to it, resulting in:
<p title="You've hovered over me!" class="hover" disabled="false">Paragraph tag here!</p>
Here's the syntax:
attribute=value|another_attribute=another_value
However, do make sure that there's no spacing between the pipe symbol and attributes. -
removeAttr
TheremoveAttrmethod removes attributes from the targeted element. Similarly tosetAttr, it's able to work differently than what might be expected. Following on from the previous example forsetAttr, where three attributes (title,class,disabled) had been set to a<p>tag, here's how theremoveAttrmethod would be used to remove those attributes from the<p>tag:document.ready(function() { var paragraph = document.search("p"); // Let's imagine there's only one <p> tag on the page paragraph.press(function() { this.removeAttr("title|class|disabled"); }); });
Quite simply, you'd provide the name of attributes you want to remove and separate them with the pipe symbol. If it's the matter of removing one attribute, don't provide any pipes! :)
-
getAttr
ThegetAttrmethod really has no special algorithm behind it to make it any special, so what it does, frankly, is return the value of the attribute that you selected from the targeted element. Following on from the previous examples, here's how you may use this method:document.ready(function() { var paragraph = document.search("p"); // Let's imagine there's only one <p> tag on the page paragraph.press(function() { alert(this.getAttr("title")); // Of course, attempt to get the attribute of an element once you've actually set it! }); });
search
Thesearchmethod is essentiallydocument.querySelectorAll,document.getElementsByNameanddocument.getElementsByTagNameworking together to make it easier for you to manipulate HTML elements on the web page. As you may have noted from previous examples, just provide the name of the tag that you want to manipulate and you'll get it returned. Just like jQuery, you can supply#idor.classto target elements by their ID or class attributes.
Let's imagine that our one <p> tag has the class hover assigned to it, maybe because it's been clicked on. Because of that, we can reference the <p> tag via the hover class, like this:
document.ready(function() {
var new_p_not = document.search(".hover");
});
If there's only one instance of the .hover class in the DOM, like in this example, JSLib will bring the matched element out of the Array which document.querySelectorAll returns it in. Also note that if a search for an element returns many instances, you will have to loop through them to manipulate them. This can be done through while and for loops.
13 . trim
The trim method will remove any whitespace from the start and end of the linked string. That's basically it. Here's how you may use it:
document.ready(function() {
var lots_of_spaces = " Wow, look at the whitespace! ";
alert(lots_of_spaces.trim());
});
Once the webpage is loaded, you'd get "Wow, look at the whitespace!" alerted without any spaces before and after (does the alert function even show whitespace?!).