Developer News, More

4 JAVASCRIPT CODE SNIPPETS FOR WEB PROJECTS

new-javascript-libraries

The practice of frontend web development has grown into an enormous topic of discussion.In this post we have put together a list of 4 awesome JAVASCRIPT CODE SNIPPETS for JavaScript developers.All of these code snippets support all web browser.

JAVASCRIPT CODE SNIPPETS FOR WEB PROJECTS

Detecting the Operating System

var system = navigator.appVersion;
if (navigator.appVersion.indexOf("Mac") != -1 ) OS = "Mac";
else if (navigator.appVersion.indexOf("PowerPC") != -1 ) OS = "Mac";
else if (navigator.appVersion.indexOf("Win") != -1 ) OS = "Win";
else if (navigator.appVersion.indexOf("SunOS") != -1 ) OS = "Solaris";
else  OS = "Linux";

//Determine Browser Version
bName = navigator.appName;
bVer  = parseInt(navigator.appVersion);

if (OS == "Mac" && bName=="Netscape") { 
  // your code here
}
else if (OS =="Mac" && bName=="Microsoft Internet Explorer") { 
  // your code here
}
else if (OS =="Win" || OS == "Linux" && bName == "Netscape") {
  // your code here
}
else if (OS =="Solaris" && bName=="Netscape") {
  // your code here
}
else if (OS =="Win" || OS == "Linux" && bName=="Microsoft Internet Explorer") {
  // your code here
}

Source

 

Obtain Select Input Value

You can use this snippet for validating your code or submitting content via Ajax. There are a number of reasons to access form data before submission. Or to even stifle the submission process and pass the data manually into a backend script! The options are practically limitless.

<select id="menulist">
  <option value="1">test1</option>
  <option value="2" selected="selected">test2</option>
  <option value="3">test3</option>
</select>

<script>
var e = document.getElementById("menulist");
var optvalue = e.options[e.selectedIndex].value; // the option value attribute
var opttext  = e.options[e.selectedIndex].text; // internal text inside option element
</script>

Source

 

Get URL Parameter

This is a custom JavaScript function written with just a few lines of code. There may be times where you need to access the parameters passed into your browser via URL. This function will setup an array of results paired into name/value sets.

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

Source

 

Browser Detection

This quick snippet of code is easily digestible for advanced and beginning developers. The browser detection script can even include older Netscape browsers for people running that engine.

// Internet Explorer
var ie  = document.all != null;  //ie4 and above
var ie5 = document.getElementById && document.all;
var ie6 = document.getElementById && document.all&&(navigator.appVersion.indexOf("MSIE 6.")>=0);

// Netscape
var ns4 = document.layers != null;
var ns6 = document.getElementById && !document.all;
var ns  = ns4 || ns6;

// Firefox
var ff  = !document.layers && !document.all;

// Opera
var op  = navigator.userAgent.indexOf("opera")>0;
var op7 = op && operaVersion() <= 7;
var op8 = op && operaVersion() >= 8;

// Detects the Opera version
function operaVersion() {
	agent = navigator.userAgent;
	idx = agent.indexOf("opera");	
	if (idx>-1) {
		return parseInt(agent.subString(idx+6,idx+7));
	}
}

Source

You Might Also Like

One comment

Leave a Reply