11 JavaScript Code Snippets for Dynamic Web Projects


1. Open a Popup Window

[php]
window.open("http://webdesignledger.com","newWindow");
[/php]

2. Detect When Browser is Closed

[php]
function getUrlParam(name) {
var results = new RegExp(‘[\?&]’ + name + ‘=([^&#]*)’).exec(window.location.href);
return (results && results[1]) || undefined;
}

[/php]

3. Obtain Select Input Value

[php]
function getUrlParam(name) {
var results = new RegExp(‘[\?&]’ + name + ‘=([^&#]*)’).exec(window.location.href);
return (results && results[1]) || undefined;
}

[/php]

4. Browser Detection

[php]
function getUrlParam(name) {
var results = new RegExp(‘[\?&]’ + name + ‘=([^&#]*)’).exec(window.location.href);
return (results && results[1]) || undefined;
}

[/php]

5. JavaScript Cookies

[php]
function getUrlParam(name) {
var results = new RegExp(‘[\?&]’ + name + ‘=([^&#]*)’).exec(window.location.href);
return (results && results[1]) || undefined;
}

[/php]

6. Detecting the Operating System

[php]
function getUrlParam(name) {
var results = new RegExp(‘[\?&]’ + name + ‘=([^&#]*)’).exec(window.location.href);
return (results && results[1]) || undefined;
}

[/php]

7. Detecting Mobile Devices

[php]
function getUrlParam(name) {
var results = new RegExp(‘[\?&]’ + name + ‘=([^&#]*)’).exec(window.location.href);
return (results && results[1]) || undefined;
}

[/php]

8. Browser Window Dimensions

[php]
function getUrlParam(name) {
var results = new RegExp(‘[\?&]’ + name + ‘=([^&#]*)’).exec(window.location.href);
return (results && results[1]) || undefined;
}

[/php]

9. Get URL Parameter

[php]
function getUrlParam(name) {
var results = new RegExp(‘[\?&]’ + name + ‘=([^&#]*)’).exec(window.location.href);
return (results && results[1]) || undefined;
}

10. JavaScript ‘Back’ Button
[php]
<a href="javascript:history.back(1)">Previous Page</a>

[/php]

11. Check Type of JS Object

[php]
function objIs(type, obj) {
var clas = Object.prototype.toString.call(obj).slice(8, -1);
return obj !== undefined && obj !== null && clas === type;
}

objIs(‘String’, ‘test’); // true
objIs(‘String’, new String(‘test’)); // true

[/php]

Leave a comment