Skip to content

Instantly share code, notes, and snippets.

View luisenriquecorona's full-sized avatar
😎
I may be slow to respond.

TextKi JS luisenriquecorona

😎
I may be slow to respond.
View GitHub Profile
@luisenriquecorona
luisenriquecorona / parseXML.js
Last active November 3, 2019 02:05
In general, parsing XML requires a great deal of effort on the part of the JavaScript programmer. Aside from knowing the particulars of the structure ahead of time, you must also know exactly how to pull apart that structure and painstakingly reassemble it into a JavaScript object.
var users = [];
var userNodes = responseXML.getElementsByTagName('users');
var node, usernameNodes, usernameNode, username,
realnameNodes, realnameNode, realname,
emailNodes, emailNode, email;
for (var i = 0, len = userNodes.length; i < len; i++) {
node = userNodes[i];
username = realname = email = '';
usernameNodes = node.getElementsByTagName('username');
if (usernameNodes && usernameNodes[0]) {
@luisenriquecorona
luisenriquecorona / xhrPost.js
Created November 3, 2019 01:08
As you can see in this example, we do nothing if the post fails. This is usually fine when XHR is used to capture broad user statistics, but if it’s crucial that the data makes it to the server, you can add code to retry on failure:
function xhrPost(url, params, callback) {
var req = new XMLHttpRequest();
req.onerror = function() {
setTimeout(function() {
xhrPost(url, params, callback);
}, 1000);
};
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (callback && typeof callback === 'function') {
@luisenriquecorona
luisenriquecorona / getLatesPacketInterval.js
Created November 2, 2019 01:24
As MXHR responses grow larger, it becomes necessary to process each resource as it is received, rather than waiting for the entire response. This can be done by listening for readyState 3:
var req = new XMLHttpRequest();
var getLatestPacketInterval, lastLength = 0;
req.open('GET', 'rollup_images.php', true);
req.onreadystatechange = readyStateHandler;
req.send(null);
function readyStateHandler{
if (req.readyState === 3 && getLatestPacketInterval === null) {
// Start polling.
getLatestPacketInterval = window.setInterval(function() {
getLatestPacket();
@luisenriquecorona
luisenriquecorona / handleImageData.js
Created November 2, 2019 01:17
Any data type that can be handled as a string by JavaScript can be sent. Here are functions that will take strings for JavaScript code, CSS styles, and images and convert them into resources the browser can use:
function handleImageData(data, mimeType) {
var img = document.createElement('img');
img.src = 'data:' + mimeType + ';base64,' + data;
return img;
}
function handleCss(data) {
var style = document.createElement('style');
style.type = 'text/css';
var node = document.createTextNode(data);
style.appendChild(node);
@luisenriquecorona
luisenriquecorona / req.responseText.js
Created November 2, 2019 00:54
By far the most common technique used, XMLHttpRequest (XHR) allows you to asyn- chronously send and receive data.
var url = '/data.php';
var params = [
'id=934875',
'limit=20'
];
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (req.readyState === 4) {
var responseHeaders = req.getAllResponseHeaders(); // Get the response
headers.
@luisenriquecorona
luisenriquecorona / Ping.js
Created October 1, 2019 00:29
Ping to server
function moveon() {
// Display a modal dialog to ask the user a question
var answer = confirm(“Ready to move on?”);
// If they clicked the “OK” button, make the browser load a new page
if (answer) window.location = “http://google.com“;
}
// Run the function defined above 1 minute (60,000 milliseconds) from now.
setTimeout(moveon, 60000);
@luisenriquecorona
luisenriquecorona / Symbols.js
Created September 19, 2019 23:13
The Symbol.match, Symbol.replace, Symbol.search, and Symbol.split Properties
// effectively equivalent to /^.{10}$/
let hasLengthOf10 = {
[Symbol.match]: function(value) {
return value.length === 10 ? [value.substring(0, 10)] : null;
},
[Symbol.replace]: function(value, replacement) {
return value.length === 10 ? replacement + value.substring(10) : value;
},
[Symbol.search]: function(value) {
return value.length === 10 ? 0 : -1;
function getFlags(re) {
var text = re.toString();
return text.substring(text.lastIndexOf("/") + 1, text.length);
}
// toString() is "/ab/g"
var re = /ab/g;
console.log(getFlags(re)); // "g"
let re = /ab/g;
console.log(re.source); // "ab"
@luisenriquecorona
luisenriquecorona / Expressions.js
Last active July 21, 2019 07:06
The Regular Expressions & Flag
let text = "hello1 hello2 hello3",
pattern = /hello\d\s?/,
result = pattern.exec(text),
globalPattern = /hello\d\s?/g,
globalResult = globalPattern.exec(text),
stickyPattern = /hello\d\s?/y,
stickyResult = stickyPattern.exec(text);
console.log(result[0]);
console.log(globalResult[0]);
console.log(stickyResult[0]);
//The codePointAt() Method
let text = "𠮷";
console.log(text.length);
console.log(/^.$/.test(text));
console.log(text.charAt(0));
console.log(text.charAt(1));
console.log(text.charCodeAt(0));
console.log(text.charCodeAt(1));
let text = "𠮷a";