Categories
HTML Java Script Jquery Web & Programming

Objects and Privacy in Javascript

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

We all know that Javascript does not support (property or method) privacy when you write Object Oriented code. In this post, i will explain how to create private (properties and methods) in Javascript.

Lets start now to do an experiment: lets create a normal object with a property and method, and try to access them to see what we get.

Here is how to create a simple object:

// Creating an Object:
var myObject = {};

// Now setting a method and a property
myObjcet.myProperty = 10;
myObject.myMethod = function (){return 'My Method';};

Now, lets try to access them. I will use my Console on the browser:

console.log(myObject.myProperty);
// This will return 10

console.log(myObject.myMethod());
// This will return 'My Method' string

We can modify our method and property by doing this:

myObject.myProperty = 15;
myObject.myMethod = function (){return 'something else';};

This will be a problem, because we need to access our method and property.. But we don’t want them to be changed or modified.
Let’s try to write this in a different  way to make them private.

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

var myObject = (function (){
   // this is a private property. Cannot be accessed.
   var privateVar = 10;

   return {
      myProperty = 15,
      myMethod = function (){return 'my public method';}
   };
}());

Now, we can access “myProperty” and “my Method” as the following code:

console.log(myObject.myProperty); // 15
console.log(myObject.myMethod()); // "my public method"

But we can’t access the “privateVar” property. It can be only accessed within the Object scope.
Let’s try to get its value. We will need to write another method to do that:

//Same as previous declaration:
var myObject = (function (){
   // this is a private property. Cannot be accessed.
   var privateVar = 10;

   return {
      myProperty : 15,
      myMethod : function (){return 'my public method';},
      // Here we can get the private value
      myVar : function (){return privateVar;}
   };
}());

Now, if we tried to access all that from our Console, we will get :

console.log(myObject.privateVar);
// undefined

console.log(myObject.myVar);
// 10

There is another way to do this, but it’s not supported by all browsers (supported by Firefox, Chrome, Safari, IE9+ and somehow supported by IE8)
By using:

 

Categories
Browsers CSS DHTML HTML Java Script Jquery Web & Programming

Building and Testing your JavaScript app with Yeoman

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

I really like automation, especially when its smart.
I am talking here about building applications, testing, building for production and some other process may needed.

We will talk here about these tools:
Bower: is a dependencies manager. Managing your app by simply installing packages needed to your app.
GruntJS: is a Build and Testing tool. So, you can build your application, unit testing, minify CSS and JS files… etc.

Yeoman came to combine these 2 tools and some other tools into one. Yeoman job is to create a development application with a standard structure of files and directories.

jQuery, RequireJS and HTML5 Boilerplate are included by default with Yeoman to build your application.

This structure is like:

Yeoman files and directories

As you see, its using HTML5 boilerplate structure.

How to install Yeoman?

First, you will need to know that NodeJS is required. So, you will need to install it first.
To install Yeoman, write this in your command line:

npm install -g yo

How to build application with Yeoman?

Once its installed, all you have to do is:In your command line, go to the directory (folder) you want to setup your application in. Then, run this command:

yo webapp

Note: the setup directory should be empty, or you will get errors.

After building your application, you can start writing code and developing your app.

How to test my application?

You will need to run this command:

grunt server

It will prepare things and will open the browser for you to see your app.

Note: Make sure that port 9000 is not used by any application, while grunt is using it for testing.

While you are testing, don’t close the command line, and you won’t need to refresh the page. Grunt will do that for you when you update files.

How to build my application for production?

All you need is to run this command:

grunt build

Note: You will need to wait for a minute or two till this process is done.

You will see new folder in your application called dist that contains all built files.

grunt build

You see that script files are combined into one file. And every time you run the build command, it will create another file version for you.

How to install all dependencies and packages for my application?

You will need to know about this when you update your dependencies, or when you install the app in another place.
Because you don’t have to download all libraries and dependencies manually, your app will be clean, simple and be installed anywhere.

All you have to do when you want to install these dependencies is to run this:

bower install

If you already have dependencies installed, and you changed something or you need to update them, run this command:

bower update

But, How to tell my applications about dependencies and requirements?

You will need to update you bower.js highlighted with yellow here:

Bower.js file

For more information, please read:

Enjoy

 

Categories
Bootstrap CSS HTML Java Script Jquery touch Web & Programming

Overriding Javascript in Twitter Bootstrap

You all know about Twitter Bootstrap. It’s the top front-end development framework and it’s spreading more and more. And while this framework was made on standards and most of useful components were added to it, some of us may need to override some code of it for his/her needs.

I had a chance to work on that, i mean overriding. Let me explain what I was doing and how I did that.

I was working on Left Tabs explained below:

left-tabs

I put a Youtube video in the Tab Content side and when screen size gets smaller, the video gets smaller. This is a really bad behavior for using this.
So, I added an exceptional case for Content to get beneath the tabs in small screens.

In HTML, I made this:

 
<div class="tabbable tabs-left">
	<ul class="nav nav-tabs">
		<li class="active"><a href="#tab1" data-toggle="tab">
			<span class="video-title">1st video</span>
		</a></li>
		<li><a href="#tab2" data-toggle="tab">
			<span class="video-title">2nd video</span>
		</a></li>
		<li><a href="#tab3" data-toggle="tab">
			<span class="video-title">3rd video</span>
		</a></li>
	</ul>
	<!-- Here is what i've added -->
	<div class="clearer"></div>
	<div class="tab-content">
		<div class="tab-pane active" id="tab1">
			<div class="tab-content">
				<p>
					<h4>1st video</h4>
				</p>
				<iframe width="98%" height="400" src="//www.youtube.com/embed/Sui1IDorBsQ?rel=0" frameborder="0" allowfullscreen></iframe>
			</div>
		</div><!-- #tab1 -->
		<div class="tab-pane" id="tab2">
			<div class="tab-content">
				<p>
					<h4>2nd video</h4>
				</p>
				<iframe width="98%" height="400" src="//www.youtube.com/embed/Sui1IDorBsQ?rel=0" frameborder="0" allowfullscreen></iframe>
			</div>
		</div><!-- #tab2 -->
		<div class="tab-pane" id="tab3">
			<div class="tab-content">
				<p>
					<h4>3rd video</h4>
				</p>
				<iframe width="98%" height="400" src="//www.youtube.com/embed/Sui1IDorBsQ?rel=0" frameborder="0" allowfullscreen></iframe>
			</div>
		</div><!-- #tab3 -->
	</div><!-- .tab-content -->
</div><!-- .tabbable -->

The CSS used for the <div class="clearer"></div> is here:

 
@media (max-width:480px){
.clearer {
  *zoom: 1;
}
.clearer:before, .clearer:after {
  display: table;
  content: "";
  line-height: 0;
}
.clearer:after {
  clear: both;
}
}

Why was all that?
I needed to use Left Tabs, and clear the float when viewing in a small screen like mobile.

The problem I’ve got is:
When I click on tabs, when tab content are not in view area, I feel nothing happens. While it’s working fine. But its not scrolling down to tab content.

I tried to add scrolling on Click event, but it didn’t work. The reason is, they are using Event Delegation.

I fixed this with the code below:

// this line will remove the Previous event.
$(document).off('click.tab.data-api');
// This will add event but wthout e.preventDefault(); line
$(document).on('click.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) {
    $(this).tab('show');
});

The Bootstrap code was:

$(document).on('click.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) {
    e.preventDefault()
    $(this).tab('show')
})

The bootstrap JS file I am talking about is: https://github.com/twbs/bootstrap/blob/master/js/tab.js scroll to the end of file to line 131

Resources for solution:

 

Categories
AJAX Browsers CSS Design DHTML E-Commerce Free Stuff HTML Java Script Jquery Misc Network PHP SEO Web & Programming Web Browsers XHTML

Tips for Better Website Performance

I am here not talking about Server-side improvements. I am here talking about Front-End improvements that really affects the web page speed.

The time spent till the website is running on the client’s browser. If we analyze that time, we will get:

Categories
Browsers CSS Design DHTML Free Stuff HTML Java Script Jquery Misc News Web & Programming Web Browsers

How to: Drag and Drop with HTML5

Drang n’ Drop in HTML 5 is supported by: IE9+, Firefox 3.6+, Safari 4+, Opera 11+ and Chrome 12+. But its not supported yet for touch devices like Touch Mobiles and Tablets.
To make things more clear, its not only HTML 5.. Its a combination between HTML 5, CSS 3, and Javascript.

Categories
AJAX DHTML Free Stuff HTML Java Script Jquery PHP Web & Programming Web Browsers XHTML

How to convert HTML Forms into AJAX simply

Hello reader,

You may have a normal HTML Form, like: contact form, feedback form or any other forms in your website or any website you work on.
Did you think how difficult converting this FORM into AJAX?

Let me say, it will be very simple. Simple as 1 minute of changes.

How is that, right?

Categories
CSS Design DHTML Free Stuff Java Script Jquery Web & Programming Web Browsers

jQuery Animate – Advanced

In the last jQuery Animate article I wrote, I mentioned how to use the .animate() function with few tricks.

But here, I am going to write about jQuery Animate function in general and I will explain few things and some tricks.

The jQuery Animate is a smart object because could be used at any syntax.
I mean,
We can use the default syntax of this function as follow:

$().animate({
/* params */ },
/* integet time period in milliseconds */
);

Categories
Browsers CSS Design DHTML Free Stuff Java Script Jquery Web & Programming

jQuery Animate

Here I will mention how to use the jquery animate function and the usability and flexibility of it.
The syntax of this function is:

$.animate(options, settings, callback);
/* you can replace setting with duration (in milliseconds) or words: 'slow', 'normal' or 'fast'.
callback is optional */

Below is a list of popular and most used animation:

  • increasing/decreasing width and height
  • moving or scrolling html element
  • changing opacity
  • scrolling window

Now let me explain each in an example.