Advertisements

Archive

Posts Tagged ‘Cascading Style Sheets’

Bundling and minification in MVC3 and Asp.Net 4.0

March 6, 2013 1 comment

Adding references:

First of all add the references of System.Web.Optimization.dll and WebGrease.dll to your MVC3 and Asp.Net 4.0 projects as shown below. You can download the dll by using download link.


Creating Bundle:

Now create the bundle for your css and js files with in the Global.asax file as shown below.

Here, I have created the bundle of all required css and js files. You can also add your own css and js files with complete path using Include method.

Registering Bundle:

Adding Bundles to Layout Page in MVC3:

Now you can add the above created style and script bundles to the Layout page or where you want to use as shown below:

Adding Bundles to Master Page in Asp.Net 4.0:

Now you can add the above created style and script bundles to the Master page or where you want to use as shown below:

In Asp.Net 4.0 you also required to add System.Web.Optimization namespace and assembly Microsoft.AspNet.Web.Optimization.WebForms reference to the web.config file of your Asp.Net 4.0 project as shown below:

You need not to do any changes in web.config file of your MVC3 project.

Enabling Bundling and Minification in debug mode

Bundling and minification doesn’t work in debug mode. So to enable this features you need to add below line of code with in Application_Start event of Global.asax.

How it works..

Now run your application and you will see that all the css and js files are converted to single css and js file as shown below:

Minification

Minification is technique for removing unnecessary characters (like white space, newline, tab) and comments from the JavaScript and CSS files to reduce the size which cause improved load times of a webpage. There are so many tools for minifying the js and css files. JSMin and YUI Compressor are two most popular tools for minifying the js and css files. Use these tools for minifiying your css and js files and use in your application with “.min” suffix. So that you can easily identified that this is a minimize version of your css or js file.

Hope it helps!

Advertisements

twiter bootstrap Navbar page selection (class=’active’)

March 6, 2013 6 comments

Hello All,

I have noticed a very common problem in twitter-bootstrap navbar , when user click on any item or navigate on other page by clicking any item from navbar, item not remain selected/active.

Here is the solution for above problem
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<meta charset=”utf-8″>
<title>AXSyst</title>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<meta name=”description” content=”Advanced Xpert Systems”>
<meta name=”author” content=””>
<!– Le styles –>
<link href=”css/bootstrap.css” rel=”stylesheet”>
<style type=”text/css”>
  body {
    padding-top: 60px;
    padding-bottom: 40px;
  }
</style>
<link href=”css/bootstrap-responsive.css” rel=”stylesheet”>
<!– Le HTML5 shim, for IE6-8 support of HTML5 elements –>
<!–[if lt IE 9]>
<![endif]–>
<script src=”js/jquery.js”></script>  <!–THIS NEEDS TO BE PLACED HERE BECAUSE $(document).ready(function(){ WON’T WORK.  ALL OTHER JS ARE LOCATED IN _footer.php –>
</head>
<body>
<!–NavBar –>
<div>
  <div>
    <div>
      <a data-toggle=”collapse” data-target=”.nav-collapse”>
        <span></span>
        <span></span>
        <span></span>
      </a>
      <a href=”index.php”>MySite</a>
      <div>
        <ul id=ul_nav>
          <li id=”li_home”><a href=”index.php”>Home</a></li>
          <li id=”li_about”><a href=”about.php”>About</a></li>
          <li id=”li_cont”><a href=”contact.php”>Contact</a></li>
        </ul>
      </div>
    </div>
  </div>
</div>
<!–NavBar end –>
Add following line of code to make selected node active in navbar….
———————————————————————————–
<script type=”text/javascript”>
function GetCurrentPageName() { 
//method to get Current page name from url. 
var PageURL = document.location.href; 
var PageName = PageURL.substring(PageURL.lastIndexOf(‘/’) + 1); 
 
return PageName.toLowerCase() ;
}
 
$(document).ready(function(){
var CurrPage = GetCurrentPageName();
 
switch(CurrPage){
case ‘index.php’:
 $(‘#li_home’).addClass(‘active’) ;
 break;
case ‘about.php’:
 $(‘#li_about’).addClass(‘active’) ;
 break;
case ‘contact.php’:
 $(‘#li_cont’).addClass(‘active’) ;
 break;
}
});
</script>
———————————————————————————–
Hope it helps !