If you are looking to redirect from one webpage to another, it is recommended to utilize an anchor tag. However, if you wish to incorporate event handling, it will depend on the markup structure.
Typically, writing inline JavaScript or CSS code is not considered a good practice. Instead, it is advised to use classes or IDs and write custom code in a separate JavaScript file.
For instance:
<ul id="primary-nav">
<li> Home </li>
<li> About </li>
</ul>
After adding jQuery to your page, you can implement the necessary code as follows:
(function(){
$("#primary-nav").on("click", function(){
// Your Code;
})
})
Here are some examples of best practices:
HTML boilerplate
<!DOCTYPE html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"/>
<title>Your Page Title</title>
<meta name="description" content="goes here..."/>
<meta name="keywords" content="keyword 1, keyword 2"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="robots"/>
<link rel="stylesheet" href="your-single-deployment-stylesheet.css">
<link rel="apple-touch-icon-precomposed" href="apple-touch-icon-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="apple-touch-icon-72x72-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="apple-touch-icon-114x114-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="apple-touch-icon-144x144-precomposed.png">
<!-- JS Links should sit on the bottom of the page! -->
</head>
<body>
... content
<script src="yourJavascript.js"></script>
</body>
</html>
CSS
Example of code with poor naming conventions
.s-scr {
overflow: auto;
}
.cb {
background: #000;
}
Example of code with improved naming conventions
.is-scrollable {
overflow: auto;
}
.column-body {
background: #000;
}
Example of code with poor names
.right-red {
float: right;
color: red;
}
Example of code with better names
.text-highlight {
float: right;
color: red;
}
JavaScript
//avoid repeating var in variable declaration
var foo;
var bar;
var lorem;
var ipsum;
//comma seperated variable declarations
var foo,
bar,
lorem,
ipsum;
Avoid defining in multiple steps
var foo = {};
foo.prop = 'bar';
foo.method = function() {};
//defined in a single assignment
var foo = {
prop: 'bar',
method: function() {}
}
To prevent global namespace pollution, consider using the following snippet:
(function( window, undefined ) {
var foo = 1; //private variable
})( window );
Check out my other posts on GitHub for more insights on best practices at www.github.com/ananddeepsingh