JavaScript Snippets

Check and Set Optional Arguments

function Default(oArg, oVal) {return (typeof oArg !== "undefined") ? oArg : oVal;}

function myFunction(A)
{
	// set A to 42 if undefined
    A = Default(A, 42);
    return A;
}

myFunction()    === 42;
myFunction(123) === 123;

// ECMAScript 6 supports default parameters

Leading Zeros

function IntToRange(iValue, iRange) 	  
{return ("000000000" + iValue).substr(-iRange);}

function IntToRangeSigned(iValue, iRange) 
{return ((iValue < 0.0) ? "-" : "") + IntToRange(Math.abs(iValue), iRange);}

IntToRange(123, 5) === "00123";
IntToRange(123, 2) ===    "23";

Async Iframe Loading

function CreateIframe(sID, sURL, sWidth, sHeight)
{
	// create iframe with specific parameters
    var pFrame = document.createElement("iframe");
    pFrame.src                = sURL;
	pFrame.allowfullscreen    = true;
    if(sWidth)  pFrame.width  = sWidth;
    if(sHeight) pFrame.height = sHeight;

	// add it to the target area
    document.getElementById(sID).appendChild(pFrame);
}

window.addEventListener("load", function(){CreateIframe("myID", "myURL", "560px", "315px");}, false);

<div id="myID"></div>

Automatic Anchor Placement

function AddAnchors(sTag)
{
    // fetch all elements by specific tag
    var apElement = document.getElementsByTagName(sTag);   // or getElementsByClassName
    for(var i = 0; i < apElement.length; ++i)
    {
        // escape text and add as clickable anchor
        var sEscape = encodeURIComponent(apElement[i].innerHTML.replace(/ /g, "-"));
        apElement[i].innerHTML += " <a id='" + sEscape + "' href='#" + sEscape + "'>*</a>";
    }

    if(window.location.hash)
    {
 		// move to the current anchor
        var sHash = window.location.hash; 
        window.location.hash = ""; 
        window.location.hash = sHash;
    }
}

window.addEventListener("load", function(){AddAnchors("h3");}, false);

Toggle Fullscreen Mode

function ToggleFullscreen()
{
    // check for active fullscreen mode
    if(document.fullscreenElement       || document.mozFullScreenElement ||
       document.webkitFullscreenElement || document.msFullscreenElement)
    {
        // disable fullscreen mode
             if(document.exitFullscreen)       document.exitFullscreen();
        else if(document.mozCancelFullScreen)  document.mozCancelFullScreen();
        else if(document.webkitExitFullscreen) document.webkitExitFullscreen();
        else if(document.msExitFullscreen)     document.msExitFullscreen();
    }
    else
    {
        var pElement = document.documentElement;
           
        // enable fullscreen mode
             if(pElement.requestFullscreen)       pElement.requestFullscreen();
        else if(pElement.mozRequestFullScreen)    pElement.mozRequestFullScreen();
        else if(pElement.webkitRequestFullscreen) pElement.webkitRequestFullscreen();
        else if(pElement.msRequestFullscreen)     pElement.msRequestFullscreen();
    }
}

document.getElementById("myButton").addEventListener("click", ToggleFullscreen, false);

Fetch URL Parameters

var asQueryParam = function()
{
    var asOutput = {};
    var asList   = window.location.search.substring(1).split("&");

    // loop through all parameters
    for(var i = 0; i < asList.length; ++i)
    {
        // separate key from value
        var asPair = asList[i].split("=");

        // insert value into map
		if(typeof asOutput[asPair[0]] === "undefined")
			asOutput[asPair[0]] = asPair[1];                          // create new entry
        else if(typeof asOutput[asPair[0]] === "string")
			asOutput[asPair[0]] = [asOutput[asPair[0]], asPair[1]];   // extend into array
        else
			asOutput[asPair[0]].push(asPair[1]);                      // append to array
    }

    return asOutput;
}();

if(asQueryParam["myParam"]) alert(asQueryParam["myParam"]);

Maus Games


© 2012-now Martin Mauersics
This email address is being protected from spambots. You need JavaScript enabled to view it. - Legal Notice