AkkenamTechnologies

AkkenamTechnologies Akkenam Technologies is a website and software service providing company in Dindigul, tamilnadu.

19/01/2019

Resource loading: onload and onerror
The browser allows to track the loading of external resources – scripts, iframes, pictures and so on.

There are two events for it:

onload – successful load,
onerror – an error occurred.
Loading a script

Let’s say we need to call a function that resides in an external script.

We can load it dynamically, like this:

let script = document.createElement('script');
script.src = "my.js";

document.head.append(script);
…But how to run the function that is declared inside that script? We need to wait until the script loads, and only then we can call it.

script.onload

The main helper is the load event. It triggers after the script was loaded and executed.

For instance:

let script = document.createElement('script');

// can load any script, from any domain
script.src = "https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.3.0/lodash.js"
document.head.append(script);

script.onload = function() {
// the script creates a helper function "_"
alert(_); // the function is available
};
So in onload we can use script variables, run functions etc.

…And what if the loading failed? For instance, there’s no such script (error 404) or the server or the server is down (unavailable).

script.onerror

Errors that occur during the loading (but not ex*****on) of the script can be tracked on error event.

For instance, let’s request a script that doesn’t exist:

let script = document.createElement('script');
script.src = "https://example.com/404.js"; // no such script
document.head.append(script);

script.onerror = function() {
alert("Error loading " + this.src); // Error loading https://example.com/404.js
};
Please note that we can’t get error details here. We don’t know was it error 404 or 500 or something else. Just that the loading failed.

Other resources

The load and error events also work for other resources. There may be minor differences though.

For instance:

, (external stylesheets)
Both load and error events work as expected.

Only load event when the iframe loading finished. It triggers both for successful load and in case of an error. That’s for historical reasons.

18/01/2019

Display the shadow of an image – Javascript Plugin

In this post I am going to share Simple and small Javascript plugin to display the shadow of the image. The Plugin provides a simple way to apply a drop shadow effect to any image and video using pure JavaScript.
image-shadow

Libraries

Load the plugin library form CDN


HTML

All you need is .ishadow wrapper to the image and blur value in data attribute






Also, you can make an element hoverable by adding data-hover="true".
false by default



17/01/2019

Simple autocomplete pure vanilla Javascript library
autoComplete.js is a simple pure vanilla Javascript library that’s progressively designed for speed, high versatility and seamless integration with wide range of projects & systems, made for users and developers in mind. It is dynamic, blazing fast, dependency-free, and easy-to-use autocomplete & typeahead JavaScript library for modern web development.
Simple autocomplete pure vanilla Javascript library - autoComplete.js
Features:

Simple & Easy to use
Pure Vanilla Javascript
Zero Dependencies
Lightweight
Lightning Fast
Versatile
Customizable
First Class Error Handling & Reporting



Libraries

Load thr js and css library on page.

link rel="stylesheet" href="[email protected]/dist/css/autoComplete.min.css"/" rel="ugc" target="_blank">https://cdn.jsdelivr.net/gh/TarekRaafat/[email protected]/dist/css/autoComplete.min.css"/>

HTML

Assign id=”autoComplete” to the input filed




JS

Create new instance of autoComplete engine

new autoComplete({
dataSrc: grocery, // Array data source
searchEngine: "strict", // Search Engine type
threshold: 0, // Min. Chars length before Engine starts
renderResults: { // Results Destination & position
destination: document.querySelector(" "),
position: "afterend"
},
placeHolder: "Try me...", // Place Holder text
maxResults: 10, // Max number of results
highlight: true, // Highlight matching results
dataAttribute: {
tag: "set", // Data attribute tag
value: "value" // Data attribute value
},
onSelection: value => { // Action script onClick event
document.querySelector(".selection").innerHTML = value.id;
}
});

16/01/2019

Vue.js Circular Time Range Picker

I ma going to share simple Vue Component for selecting time range. This Vue.js component is helpful to select the time intervals of the day.
vue-time-picker

Template

Use following template to display Vue.js Circular Time Range Picker








JS

Call the component and enable time picker


import VueTimeRangesPicker from 'vue-time-ranges-picker';

export default {
components: {
VueTimeRangesPicker
},

data() {
return {
ranges: [
{
startTime: '00:00',
endTime: '03:00',
scaleColor: 'violet',
},
{
startTime: '03:00',
endTime: '06:00',
scaleColor: 'yellow',
},
{
startTime: '06:00',
endTime: '00:00',
scaleColor: 'aquamarine',
}
]
}
}
}>



CSS

Styling time picker

time-picker-wrapper {
width: 300px;
}

Note:

If you want to use this component in browser which doesn’t support pointer events, then enable polyfill. For example,you can add this script in the header of your html page https://code.jquery.com/pep/0.4.3/pep.js

You cal also use following to properties to customize the plugins.

{
isShowChosenTime: true,
isShowQuartersText: true,
isShowHoursMarks: true,
chosenTimeColor: 'grey',
pointerColor: 'white',
activePointerColor: 'rgba(240, 240, 240, 0.9)',
pointerRadius: 6,
activePointerRadius: 5.5,
circleStrokeWidth: 8,
hoursMarksColor: 'grey',
quarterTextColor: 'grey',
}

14/01/2019

Searching: getElement* and querySelector*
DOM navigation properties are great when elements are close to each other. What if they are not? How to get an arbitrary element of the page?

There are additional searching methods for that.

document.getElementById or just id

If an element has the id attribute, then there’s a global variable by the name from that id.

We can use it to access the element, like this:


Element



alert(elem); // DOM-element with id="elem"
alert(window.elem); // accessing global variable like this also works

// for elem-content things are a bit more complex
// that has a dash inside, so it can't be a variable name
alert(window['elem-content']); // ...but accessible using square brackets [...]

That’s unless we declare the same-named variable by our own:




let elem = 5;

alert(elem); // the variable overrides the element

The behavior is described in the specification, but it is supported mainly for compatibility. The browser tries to help us by mixing namespaces of JS and DOM. Good for very simple scripts, but there may be name conflicts. Also, when we look in JS and don’t have HTML in view, it’s not obvious where the variable comes from.

The better alternative is to use a special method document.getElementById(id).

For instance:


Element



let elem = document.getElementById('elem');

elem.style.background = 'red';

Here in the tutorial we’ll often use id to directly reference an element, but that’s only to keep things short. In real life document.getElementById is the preferred method.

There can be only one
The id must be unique. There can be only one element in the document with the given id.

If there are multiple elements with the same id, then the behavior of corresponding methods is unpredictable. The browser may return any of them at random. So please stick to the rule and keep id unique.

Only document.getElementById, not anyNode.getElementById
The method getElementById that can be called only on document object. It looks for the given id in the whole document.

getElementsBy*

There are also other methods to look for nodes:

elem.getElementsByTagName(tag) looks for elements with the given tag and returns the collection of them. The tag parameter can also be a star "*" for “any tags”.
For instance:

// get all divs in the document
let divs = document.getElementsByTagName('div');
This method is callable in the context of any DOM element.

Let’s find all input tags inside the table:



Your age:



less than 18


from 18 to 50


more than 60






let inputs = table.getElementsByTagName('input');

for (let input of inputs) {
alert( input.value + ': ' + input.checked );
}

Don’t forget the "s" letter!
Novice developers sometimes forget the letter "s". That is, they try to call getElementByTagName instead of getElementsByTagName.

The "s" letter is absent in getElementById, because it returns a single element. But getElementsByTagName returns a collection of elements, so there’s "s" inside.

It returns a collection, not an element!
Another widespread novice mistake is to write:

// doesn't work
document.getElementsByTagName('input').value = 5;
That won’t work, because it takes a collection of inputs and assigns the value to it rather than to elements inside it.

We should either iterate over the collection or get an element by its index, and then assign, like this:

// should work (if there's an input)
document.getElementsByTagName('input')[0].value = 5;
There are also other rarely used methods of this kind:

elem.getElementsByClassName(className) returns elements that have the given CSS class. Elements may have other classes too.
document.getElementsByName(name) returns elements with the given name attribute, document-wide. Exists for historical reasons, very rarely used, we mention it here only for completeness.
For instance:


Article
Long article



// find by name attribute
let form = document.getElementsByName('my-form')[0];

// find by class inside the form
let articles = form.getElementsByClassName('article');
alert(articles.length); // 2, found two elements with class "article"

13/01/2019

Walking the DOM
The DOM allows to do anything with elements and their contents, but first we need to reach the corresponding DOM object, get it into a variable, and then we are able to modify it.

All operations on the DOM start with the document object. From it we can access any node.

Here’s a picture of links that allow to travel between DOM nodes:

Let’s discuss them in more detail.

On top: documentElement and body

The topmost tree nodes are available directly as document properties:

= document.documentElement
The topmost document node is document.documentElement. That’s DOM node of tag.
= document.body
Another widely used DOM node is the element – document.body.
= document.head
The tag is available as document.head.
There’s a catch: document.body can be null
A script cannot access an element that doesn’t exist at the moment of running.

In particular, if a script is inside , then document.body is unavailable, because the browser did not read it yet.

So, in the example below the first alert shows null:





alert( "From HEAD: " + document.body ); // null, there's no yet






alert( "From BODY: " + document.body ); // HTMLBodyElement, now it exists




In the DOM world null means “doesn’t exist”
In the DOM, the null value means “doesn’t exist” or “no such node”.

11/01/2019

DOM tree
The backbone of an HTML document are tags.

According to Document Object Model (DOM), every HTML-tag is an object. Nested tags are called “children” of the enclosing one.

The text inside a tag it is an object as well.

All these objects are accessible using JavaScript.

An example of DOM

For instance, let’s explore the DOM for this document:




About elks


The truth about elks.


The DOM represents HTML as a tree structure of tags. Here’s how it looks:

On the picture above, you can click on element nodes and their children will open/collapse.

Tags are called element nodes (or just elements). Nested tags become children of the enclosing ones. As a result we have a tree of elements: is at the root, then and are its children, etc.

The text inside elements forms text nodes, labelled as . A text node contains only a string. It may not have children and is always a leaf of the tree.

For instance, the tag has the text "About elks".

Please note the special characters in text nodes:

a newline: ↵ (in JavaScript known as \n)
a space: ␣
Spaces and newlines – are totally valid characters, they form text nodes and become a part of the DOM. So, for instance, in the example above the tag contains some spaces before , and that text becomes a node (it contains a newline and some spaces only).

There are only two top-level exclusions:

Spaces and newlines before are ignored for historical reasons,
If we put something after , then that is automatically moved inside the body, at the end, as the HTML spec requires that all content must be inside . So there may be no spaces after .
In other cases everything’s straightforward – if there are spaces (just like any character) in the document, then they become text nodes in DOM, and if we remove them, then there won’t be any.

Here are no space-only text nodes:


About elksThe truth about elks.
Edge spaces and in-between empty text are usually hidden in tools
Browser tools (to be covered soon) that work with DOM usually do not show spaces at the start/end of the text and empty text nodes (line-breaks) between tags.

That’s because they are mainly used to decorate HTML, and do not affect how it is shown (in most cases).

On further DOM pictures we’ll sometimes omit them where they are irrelevant, to keep things short.

10/01/2019

Developer console
Code is prone to errors. You will quite likely make errors… Oh, what am I talking about? You are absolutely going to make errors, at least if you’re a human, not a robot.

But in the browser, users don’t see errors by default. So, if something goes wrong in the script, we won’t see what’s broken and can’t fix it.

To see errors and get a lot of other useful information about scripts, “developer tools” have been embedded in browsers.

Most developers lean towards Chrome or Firefox for development because those browsers have the best developer tools. Other browsers also provide developer tools, sometimes with special features, but are usually playing “catch-up” to Chrome or Firefox. So most developers have a “favorite” browser and switch to others if a problem is browser-specific.

Developer tools are potent; they have many features. To start, we’ll learn how to open them, look at errors, and run JavaScript commands.

Google Chrome

Open the page bug.html.

There’s an error in the JavaScript code on it. It’s hidden from a regular visitor’s eyes, so let’s open developer tools to see it.

Press F12 or, if you’re on Mac, then Cmd+Opt+J.

The developer tools will open on the Console tab by default.

The exact look of developer tools depends on your version of Chrome. It changes from time to time but should be similar.

Here we can see the red-colored error message. In this case, the script contains an unknown “lalala” command.
On the right, there is a clickable link to the source bug.html:12 with the line number where the error has occurred.
Below the error message, there is a blue > symbol. It marks a “command line” where we can type JavaScript commands. Press Enter to run them (Shift+Enter to input multi-line commands).

Now we can see errors, and that’s enough for a start. We’ll come back to developer tools later and cover debugging more in-depth in the chapter Debugging in Chrome.

Firefox, Edge, and others

Most other browsers use F12 to open developer tools.

The look & feel of them is quite similar. Once you know how to use one of these tools (you can start with Chrome), you can easily switch to another.

Safari

Safari (Mac browser, not supported by Windows/Linux) is a little bit special here. We need to enable the “Develop menu” first.

Open Preferences and go to the “Advanced” pane.

Now Cmd+Opt+C can toggle the console. Also, note that the new top menu item named “Develop” has appeared. It has many commands and options.

09/01/2019

Code editors
A code editor is the place where programmers spend most of their time.

There are two main types of code editors: IDEs and lightweight editors. Many people use one tool of each type.

IDE

The term IDE (Integrated Development Environment) refers to a powerful editor with many features that usually operates on a “whole project.” As the name suggests, it’s not just an editor, but a full-scale “development environment.”

An IDE loads the project (which can be many files), allows navigation between files, provides autocompletion based on the whole project (not just the open file), and integrates with a version management system (like git), a testing environment, and other “project-level” stuff.

If you haven’t selected an IDE yet, consider the following options:

WebStorm for frontend development. The same company offers other editors for other languages (paid).
Netbeans (free).
All of these IDEs are cross-platform.

For Windows, there’s also “Visual Studio”, not to be confused with “Visual Studio Code.” “Visual Studio” is a paid and mighty Windows-only editor, well-suited for the .NET platform. A free version of it is called Visual Studio Community.

Many IDEs are paid but have a trial period. Their cost is usually negligible compared to a qualified developer’s salary, so just choose the best one for you.

Lightweight editors

“Lightweight editors” are not as powerful as IDEs, but they’re fast, elegant and simple.

They are mainly used to open and edit a file instantly.

The main difference between a “lightweight editor” and an “IDE” is that an IDE works on a project-level, so it loads much more data on start, analyzes the project structure if needed and so on. A lightweight editor is much faster if we need only one file.

In practice, lightweight editors may have a lot of plugins including directory-level syntax analyzers and autocompleters, so there’s no strict border between a lightweight editor and an IDE.

The following options deserve your attention:

Visual Studio Code (cross-platform, free) also has many IDE-like features.
Atom (cross-platform, free).
Sublime Text (cross-platform, shareware).
Notepad++ (Windows, free).
Vim and Emacs are also cool if you know how to use them.
My favorites

The personal preference of the author is to have both an IDE for projects and a lightweight editor for quick and easy file editing.

I’m using:

As an IDE for JS – WebStorm (I switch to one of the other JetBrains offerings when using other languages)
As a lightweight editor – Sublime Text or Atom.

08/01/2019

Syntax of JavaScript does not suit everyone’s needs. Different people want different features.

That’s to be expected, because projects and requirements are different for everyone.

So recently a plethora of new languages appeared, which are transpiled (converted) to JavaScript before they run in the browser.

Modern tools make the transpilation very fast and transparent, actually allowing developers to code in another language and auto-converting it “under the hood”.

Examples of such languages:

CoffeeScript is a “syntactic sugar” for JavaScript. It introduces shorter syntax, allowing us to write clearer and more precise code. Usually, Ruby devs like it.
TypeScript is concentrated on adding “strict data typing” to simplify the development and support of complex systems. It is developed by Microsoft.
Dart is a standalone language that has its own engine that runs in non-browser environments (like mobile apps). It was initially offered by Google as a replacement for JavaScript, but as of now, browsers require it to be transpiled to JavaScript just like the ones above.
There are more. Of course, even if we use one of these languages, we should also know JavaScript to really understand what we’re doing.

07/01/2019

There are at least three great things about JavaScript:

Full integration with HTML/CSS.
Simple things are done simply.
Support by all major browsers and enabled by default.
Javascript is the only browser technology that combines these three things.

That’s what makes JavaScript unique. That’s why it’s the most widespread tool for creating browser interfaces.

While planning to learn a new technology, it’s beneficial to check its perspectives. So let’s move on to the modern trends affecting it, including new languages and browser abilities.

06/01/2019

What in-browser JavaScript do?
Modern JavaScript is a “safe” to language. It does not provide low-level access to memory or CPU, because it was initially created for browsers which do not require it.

Javascript’s capabilities greatly depend on the environment it’s running in. For instance, Node.JS supports functions that allow JavaScript to read/write arbitrary files, perform network requests, etc.

In-browser JavaScript can do everything related to webpage manipulation, interaction with the user, and the webserver.

For instance, in-browser JavaScript is able to:

Add new HTML to the page, change the existing content, modify styles.
React to user actions, run on mouse clicks, pointer movements, key presses.
Send requests over the network to remote servers, download and upload files (so-called AJAX and COMET technologies).
Get and set cookies, ask questions to the visitor, show messages.
Remember the data on the client-side (“local storage”).

What CAN’T in-browser JavaScript do?

JavaScript’s abilities in the browser are limited for the sake of the user’s safety. The aim is to prevent an evil webpage from accessing private information or harming the user’s data.

Examples of such restrictions include:

JavaScript on a webpage may not read/write arbitrary files on the hard disk, copy them or execute programs. It has no direct access to OS system functions.

Modern browsers allow it to work with files, but the access is limited and only provided if the user does certain actions, like “dropping” a file into a browser window or selecting it via an tag.

There are ways to interact with camera/microphone and other devices, but they require a user’s explicit permission. So a JavaScript-enabled page may not sneakily enable a web-camera, observe the surroundings and send the information to the NSA.

Different tabs/windows generally do not know about each other. Sometimes they do, for example when one window uses JavaScript to open the other one. But even in this case, JavaScript from one page may not access the other if they come from different sites (from a different domain, protocol or port).

This is called the “Same Origin Policy”. To work around that, both pages must contain a special JavaScript code that handles data exchange.

This limitation is, again, for the user’s safety. A page from http://anysite.com which a user has opened must not be able to access another browser tab with the URL http://gmail.com and steal information from there.

JavaScript can easily communicate over the net to the server where the current page came from. But its ability to receive data from other sites/domains is crippled. Though possible, it requires explicit agreement (expressed in HTTP headers) from the remote side. Once again, that’s a safety limitation.

Such limits do not exist if JavaScript is used outside of the browser, for example on a server. Modern browsers also allow plugin/extensions which may ask for extended permissions.

Address

Dindigul
624004

Alerts

Be the first to know and let us send you an email when AkkenamTechnologies posts news and promotions. Your email address will not be used for any other purpose, and you can unsubscribe at any time.

Contact The Business

Send a message to AkkenamTechnologies:

Share