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 / script.js
Created November 25, 2020 23:00
URLSearch.js
let qs = "?q=javascript&num=10";
let searchParams = new URLSearchParams(qs);
console.log(searchParams.toString()); // " q=javascript&num=10"
searchParams.has("num"); // true
searchParams.get("num"); // 10
searchParams.set("page", "3");
console.log(searchParams.toString()); // " q=javascript&num=10&page=3"
searchParams.delete("q");
console.log(searchParams.toString());
/**
* TOC.js: create a table of contents for a document.
*
* This module registers an anonymous function that runs automatically
* when the document finishes loading. When it runs, the function first
* looks for a document element with an id of "TOC". If there is no
* such element it creates one at the start of the document.
*
* Next, the function finds all <h1> through <h6> tags, treats them as
* section titles, and creates a table of contents within the TOC
<?
include_once("libs/smarty.class.php");
$smarty = new smarty();
$students = array(
"didar" => array("name"=>"Didar Bhuiyan","roll"=>12),
"emran" => array("name"=>"Emran Hasan","roll"=>18),
"hasan" => array("name"=>"Tanveer Hasan","roll"=>23));
$smarty->assign("students",$students);
$smarty->display("associative_array.tpl");
?>
@luisenriquecorona
luisenriquecorona / getImgAreas.js
Created May 21, 2020 23:17
A smarter scripter would recognize that the preceding scripts might fail in very old browsers, where script errors tend to be rather invasive. To prevent such errors, the author makes two modifications. First, the potentially offending script statements in the getImgAreas( ) function wrap themselves in an object detection block. Second, the main…
function getImgAreas( ) {
var result;
// make sure browser supports img element objects
if (document.images) {
// initialize return value so we can add to it
result = 0;
// loop through all img objects on the page
for (var i = 0; i < document.images.length; i++) {
// accumulate image areas
result += (document.images[i].width * document.images[i].height);
@luisenriquecorona
luisenriquecorona / cookieManager.js
Created May 21, 2020 01:35
Make use of what is known as encapsulation, an automatic feature of JavaScript objects. Each property or method name you define for an object is private to that object. For example, if you define an object that contains three properties and two methods, the only name that is accessible to other script statements outside of the object is the iden…
var cookieMgr = {
// utility function to retrieve an expiration date in proper
// format; pass three integer parameters for the number of days, hours,
// and minutes from now you want the cookie to expire (or negative
// values for a past date); all three parameters are required,
// so use zeros where appropriate
getExpDate : function(days, hours, minutes) {
var expDate = new Date( );
if (typeof days == "number" && typeof hours == "number" &&
typeof minutes == "number") {
@luisenriquecorona
luisenriquecorona / objectsArraysString.js
Created May 21, 2020 01:28
Use the objectsArraysStrings.js script library shown in the Discussion. To convert a custom object to string form, invoke the object2String( ) function, passing a refer- ence to the object as a parameter: var objAsString = object2String(myObj); To convert an array (including an array of custom objects) to string form, invoke the array2String( ) …
function object2String(obj) {
var val, output = "";
if (obj) {
output += "{";
for (var i in obj) {
val = obj[i];
switch (typeof val) {
case ("object"):
if (val[0]) {
output += i + ":" + array2String(val) + ",";