Mar 23
Variables and Methods

  • Private Variables
  • Private variables are declared with the var keyword inside the object, and can only be accessed by private functions and privileged methods.

  • Private Functions
  • Private functions are declared inside the object’s constructor, and may only be called by privileged methods.

  • Privileged Methods
  • Privileged methods are declared with this.methodName = function(){}, and may invoked by code external to the object.

  • Public Properties
  • Public properties are declared with this.variableName, and may be read/written from outside the object.

  • Public Methods
  • Public methods are defined by Classname.prototype.methodName = function(){}, and may be called from outside the object.

  • Prototype Properties
  • Prototype properties are defined by Classname.prototype.propertyName = someValue.

  • Static Properties
  • Static properties are defined by Classname.propertyName = someValue.

Normal Object

          			var name = 'Ling';
          			var city = 'Los Angeles';
          			var myObj = {
          				city: 'San Francisco',
          				go: function(name) {
          					// "this.city" refers to myObj.city('San Francisco'). "city" refers to 'Los Angeles'
          					var str = name + ' is going to ' + city + '.';
          					return str;
          				}
          			};
          			var obj = myObj;
          			obj.city = 'Seattle';
          			obj.do = function(name) {
          				var str = name + ' is watching the show in ' + this.city + '.';
          				return str;
          			}
          			console.log(obj.go('Ping')); => Ping is going to Los Angeles.
          			console.log(obj.do('Ping')); => Ping is watching the show in Seattle.
          			console.log(myObj); => {city:'Seattle', do:function(name){}, go:function(name){}}
          		
Function Object and Prototype Property

  • Example One
  •           				var name = 'Ling';
              				var city = 'Los Angeles';
              				var myObj = function() {
              					var city = 'San Francisco';
              					var go = function(name) {
              						var str = name + ' is going to ' + city + '.';
              						return str;
              					};
              					// Allow to access the method from outside the "myObj" object.
              					this.go = go;
              				};
              				myObj.prototype.do = function(name) {
              					// "this.city" refers to 'Seattle'. "city" refers to 'Los Angeles'.
              					var str = name + ' is watching the show in ' + city + '.';
              					return str;
              				};
              				var obj = new myObj();
              				obj.city = 'Seattle';
              				obj.come = function(name) {
              					var str = name + ' is coming back from ' + this.city + '.';
              					return str;
              				};
              				var newobj = new myObj();
              				console.log(obj.go('Ping')); => Ping is going to San Francisco.
              				console.log(obj.do('Ping')); => Ping is watching the show in Los Angeles.
              				console.log(obj.come('Ping')); => Ping is coming back from Seattle.
              				console.log(obj); => {city:'Seattle', come:function(name){}, do:function(name){}, go:function(name){}}
              				console.log(newobj); => function do(name) {}, function go(name) {}
              			
  • Example Two
  •           				var myObj = function(name) {
              					var city = 'San Francisco';
              					var go = function(name) {
              						var str = name + ' is going to ' + city + '.';
              						return str;
              					};
              					this.name = name;
              					this.go = go;
              				};
              				var obj = new myObj('Ling');
              				obj.name = 'Ping';
              				console.log(obj.go()); => Ling is going to San Francisco.
              				console.log(obj); => name:'Ping', function go() {}
              			
Inheritance

  • Method One: Via The Prototype Property
  •           				var parentObj = function() {
              					var name = 'Ling';
              					var city = 'San Francisco';
              					var go = function() {
              						var str = name + ' is going to ' + city + '.';
              						return str;
              					};
              					this.name = name;
              					this.go = go;
              				};
              				var childObj = function() {
              					var name = 'Ping';
              					var city = 'Los Angeles';
              					var come = function() {
              						// "this.name" refers to 'Ling'. "name" refers to 'Ping'.
              						var str = name + ' is coming back from ' + city + '.';
              						return str;
              					};
              					this.come = come;
              				};
              				childObj.prototype = new parentObj();
              				var obj = new childObj();
              				var newobj = new parentObj();
              				console.log(obj.go()); => Ling is going to San Francisco.
              				console.log(obj.come()); => Ping is coming back from Los Angeles.
              				console.log(obj); => name:'Ling', function come(){}, function go(){}
              				console.log(newobj); => name:'Ling', function go(){}
              			
  • Method Two: Singleton
  • Purpose: If you have several constructor functions that willl inherit parentObj objects, you may create new parentObj() every time, but it’s not necessary. Another way to do the same would be to create one (singleton) normal object and use it as a base for the other objects.

              				var parentObj = {
              					name: 'Ling',
              					city: 'San Francisco',
              					go: function() {
              						var str = name + ' is going to ' + city + '.';
              						return str;
              					}
              				};
              				var childObj = function() {
              					var name = 'Ping';
              					var city = 'Los Angeles';
              					var come = function() {
              						var str = name + ' is coming back from ' + city + '.';
              						return str;
              					};
              					this.come = come;
              				};
              				childObj.prototype = parentObj;
              				var obj = new childObj();
              				var newobj = parentObj;
              				console.log(obj); => city:'San Francisco', name:'Ling', function come(){}, function go(){}
              				console.log(newobj); => city:'San Francisco', name:'Ling', function go(){}
              			
  • Method Three: Copy Properties
  •           				function extend(parent, child) {
              					for (var key in parent) {
              						child[key] = parent[key]
              					return child;
              				}
              					var parentObj = {
              					name: 'Ling',
              					city: 'San Francisco',
              					go: function() {
              						var str = name + ' is going to ' + city + '.';
              						return str;
              					}
              				};
              				var childObj = {
              					name: 'Ping',
              					city: 'Los Angeles',
              					come: function() {
              						var str = name + ' is coming back from ' + city + '.';
              						return str;
              					}
              				};
              				var obj = extend(parentObj, childObj);
              				var newobj = parentObj;
              				console.log(obj); => {city:'San Francisco', name:'Ling', come:function(){}, go:function(){}}
              				console.log(newobj); => {city:'San Francisco', name:'Ling', go:function(){}}
              			
  • Method Four: Crockford’s Beget Object
  • Idea: Create a temp constructor so you can use the prototype functionality, the idea is that you create a new object, but instead of starting fresh, you inherit some functionality from another, already existing, object. Then augment the new object with more functionality.

              				function begetObject(o) {
              					function F() {}
              						F.prototype = o;
              					return new F();
              				}
              				var parentObj = {
              					name: 'Ling',
              					city: 'San Francisco',
              					go: function() {
              						var str = name + ' is going to ' + city + '.';
              						return str;
              					}
              				};
              				var childObj = begetObject(parentObj);
              				childObj.name = 'Ping';
              				childObj.come = function() {
              					var str = name + ' is coming back from ' + city + '.';
              					return str;
              				}
              			
  • Method Five: Function.call() and Function.apply()
  •           				var parentObj = function(name, city) {
              					var name = name;
              					var city = city;
              					var go = function() {
              						var str = name + ' is going to ' + city + '.';
              						return str;
              					};
              					this.go = go;
              				};
              				var childObj = function(name, city) {
              					var name = 'Ping';
              					var come = function() {
              						var str = name + ' is coming back from ' + city + '.';
              						return str;
              					};
              					this.come = come;
              					parentObj.call(this, name, city);
              				};
              				var obj = new childObj('Ling', 'San Francisco');
              				console.log(obj.go()); => Ping is going to San Francisco.
              			
              				var parentObj = function(name, city) {
              					var name = name;
              					var city = city;
              					var go = function() {
              						var str = name + ' is going to ' + city + '.';
              						return str;
              					};
              					this.go = go;
              				};
              				var childObj = function(name, city) {
              					var name = 'Ping';
              					var come = function() {
              						var str = name + ' is coming back from ' + city + '.';
              						return str;
              					};
              					this.come = come;
              					parentObj.apply(this, arguments);
              				};
              				var obj = new childObj('Ling', 'San Francisco');
              				console.log(obj.go()); => Ping is going to San Francisco.
              			
Related Posts

Mar 15
Load New Document Into iFrame

  • Method 1
  •           				// For Older Browsers
              				window.frames[iframeName].location = url;
              			
  • Method 2
  •           				document.getElementById(iframeId).src = url;
              			
Access Elements and Variables

  • Access Child From Parent
  •           				// Access Variables
              				window.frames[iframeName].variableName;
              				// Access Elements
              				window.frames[iframeName].document.getElementById(id);
              			
              				// For IE5.5+ and Modern Browsers
              				// Access Variables
              				document.getElementById(iframeId).contentWindow.document.variableName;
              				// Access Elements
              				document.getElementById(iframeId).contentWindow.document.getElementById(id);
              			
              				// For Modern Browsers Only
              				document.getElementById(iframeId).contentDocument.getElementById(iframeId);
              			
  • Access Parent From Child
  •           				// Access Variables
              				parent.variableName;
              				// Access Elements
              				parent.document.getElementById(id);
              			
Dynamically Generate an iFrame

          			var IFrame = function(id, width, height, parentElement, args) {
          				var self = this;
          				var element;
          				this.width = 300;
          				this.height = 500;
          				this.frameBorder = 0;
          				this.marginWidth = 0;
          				this.marginHeight = 0;
          				this.scrolling = "auto";
          				var initialize = function() {
          					if (!id) return;
          					if (typeof id.nodeType == 1)
          						element = id;
          					else
          						element = document.createElement("iframe");
          					var Id = id;
          					if (typeof Id != "string") Id = new Date().getTime();
          					element.setAttribute("id", Id);
          					element.setAttribute("name", Id);
          					if (width) self.width = width;
          					if (height) self.height = height;
          					if (parentElement)
          						parentElement.appendChild(element);
          					else
          						document.body.appendChild(element);
          					if (args) {
          						for (var property in args) 
          							self[property] = args[property];
          						for (var key in setProperty)
          							setProperty[key]();
          					}
          				};
          				var setProperty = {
          					width: function(value) {
          						if (value) self.width = value;
          						element.style.width = self.width + "px";
          					},
          					height: function(value) {
          						if (value) self.height = value;
          						element.style.height = self.height + "px";
          					},
          					frameBorder: function() {
          						element.setAttribute("frameBorder", self.frameBorder);
          					},
          					marginWidth: function() {
          						element.setAttribute("marginWidth", self.marginWidth);
          					},
          					marginHeight: function() {
          						element.setAttribute("marginHeight", self.marginHeight);
          					},
          					scrolling: function() {
          						element.setAttribute("scrolling", self.scrolling);
          					},
          				};
          				this.setWidth = setProperty.width;
          				this.setHeight = setProperty.height;
          				this.load = function(src) {
          					if (element) {
          						element.setAttribute("src", src);
          						Generic.addEvent(element, 'load', self.setPosition);
          					}
          				};
          				this.setPosition = function() {
          					if (element) {
          						var left = (Geometry.getViewportWidth() - CSS.outerWidth(element)) / 2;
          						var top = (Geometry.getViewportHeight() - CSS.outerHeight(element)) / 2;
          						element.style.position = "relative";
          						element.style.left = left + "px";
          						element.style.top = top + "px";
          					}
          				};
          				(function() {
          					initialize();
          				})();
          			};
          		
          			window.onload = function() {
          				var iframeObj = new IFrame("myIframe", 810, 610);
          				iframeObj.load("index.html");
          			}
          		
Related Posts

Mar 08
Opacity Setting

  • With CSS
  •           				.classname {
              					opacity: 0.5; /* For Newer Mozilla Browsers, Safari, and Opera */
              					-moz-opacity: 0.5 /* For Older Mozilla Browsers, like Netscape Navigator */
              					-khtml-opacity: 0.5; /* For Older Safari */
              					-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(opacity=50)"; /* For IE 8 */
              					filter: alpha(opacity=50); /* For IE 6 and IE 7 */
              				}
              			
  • With Javascript
  •           				var setOpacity = function(element, value) {
              					element.style.opacity = value / 100; /* For Newer Mozilla Browsers, Safari, and Opera */
              					element.style.MozOpacity = value / 100; /* For Older Mozilla Browsers, like Netscape Navigator */
              					element.style.KhtmlOpacity = value / 100; /* For Older Safari */
              					element.style.MsFilter = '"progid:DXImageTransform.Microsoft.Alpha(opacity=' + value + ')"'; /* For IE8 */
              					element.style.filter = "alpha(opacity=" + value + ")"; /* For IE 6 and IE7 */
              				};
              			
Related Posts

Mar 08
Working With Backgrounds in CSS 2

The Background Shorthand Property

          			background: transparent url(images/image.png) left top scroll no-repeat;
          		

One important thing to note is that the background accounts for the contents of the element, including the padding and border. It does not include an element’s margin. This works as it should in Firefox, Safari and Opera, and now in IE8. But in IE7 and IE6 the background does not include the border.

Background Color

The background-color property fills the background with a solid color. There are a number of ways to specify the color.

					background-color: black;
					background-color: rgb(0, 0, 0);
					background-color: #000000;
				

The background-color property can also be set to transparent, which makes any elements underneath it visible instead.

Background Image

The background-image property allows you to specify an image to be displayed in the background. This can be used in conjunction with background-color, so if your image is not tiled, then any space that the image does not cover will be set to the background color. The path of the image is relative to the style sheet. So, in the following snippet, the image is in the same directory as the style sheet.

          			background-image: url(image.jpg);
          		

Background Position

The background-position property controls where a background image is located in an element. The trick with background-position is that you are actually specifying where the top-left corner of the image will be positioned, relative to the top-left corner of the element.

In the examples below, we have set a background image and are using the background-position property to control it. We have also set background-repeat to no-repeat. The measurements are all in pixels. The first digit is the x-axis position (horizontal) and the second is the y-axis position (vertical).

The background-position property also works with other values, keywords and percentages, which can be useful, especially when an element’s size is not set in pixels.

The keywords are self-explanatory. For the x-axis: left, center, right. And for the y-axis: top, center, bottom

          			background-position: left top; /* The top-left corner of the image. */
          			background-position: -10px top; /* Move the image to the left. */
          			background-position: 100% top; /* Move the image to the right. */
          			background-position: 0 -46px; /* Move the image up. */
          		

 

 

Background Attachment

The background-attachment property determines what happens to an image when the user scrolls the page. The three available properties are scroll, fixed, and inherit.

Inherit simply tells the element to follow the background-attachment property of its parent.

When we set background-attachment: scroll;, we are telling the background that when the element scrolls, the background must scroll with it. In simpler terms, the background sticks to the element. This is the default setting for background-attachment.

When we set the background-attachment to fixed, we are telling the browser that when the user scrolls down the page, the background should stay fixed where it is – i.e. not scroll with the content.

Background Repeat

By default, when you set an image, the image is repeated both horizontally and vertically until the entire elemtn is filled. This may be what you want, but sometimes you want an image to be displayed only once or to be tiled in only one direction. The possible values (and their results) are as follows:

          			background-repeat: repeat; /* The default value. Will tile the image in both dicrections. */
          			background-repeat: no-repeat; /* No tiling. The image will be used only once. */
          			background-repeat: repeat-x; /* Tiles horizontally (i.e. along the x-axis). */
          			background-repeat: repeat-y; /* Tiles vertically (i.e. along the y-axis). */
          			background-repeat: inherit; /* Uses the same background-repeat property of the element's parent. */
          		

 

How to fix PNG-24 transparency for IE6?

  • With CSS
  •           				/* For IE7 and Modern Browsers */
              				.classname {
              					background: transparent url(images/image.png) left top scroll no-repeat;
              				}
              				/* For IE6 */
              				*html .classname {
              					background-image: none;
              					filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/picture.png", sizingMethod="scale");
              				}
              			
  • With Javascript
  •           				/* For IE6 */
              				element.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="images/picture.png", sizingMethod="scale")';
              			
PNG-24 Pitfalls

  • Background Images Cannot Be Positioned Or Repeated
  • The AlphaImageLoader does work for background images, but only for the simplest of cases. If your design requires the image to be tiled (background-repeat) or positioned (background-position), you are out of luck. The AlphaImageLoader allows you to set a sizingMethod to either crop the image or to scale it to fit.

    sizingMethod Values
    Description

    crop
    Clips the image to fit the dimensions of the object.

    image
    Default. Enlarge or reduce the border of the object to fit the dimensions of the image.

    scale
    Stretch or shrink the image to fill the borders of the object.

  • Delayed Loading And Resource Use
  • The AlphaImageLoader can be quite slow to load, and appears to consume more resources than a standard image when applied. Typically, you’d need to add thousands of GIFs or JPEGs to a page before you saw any noticeable impact on the browser, but with the AlphaImageLoader filter applied Internet Explorer can become sluggish after just a handful of alpha channel PNGs.

    The other noticeable effect is that as more instances of the AlphaImageLoader are applied, the longer it takes to render the PNGs with their transparency. The user sees the PNG load in its original non-supported state (with black or grey areas where transparency should be) before one by one the filter kicks in and makes them properly transparent.

  • Links Become Unclickable, Forms Unforcusable
  • There is a bug/weirdness with AlphaImageLoader that sometimes prevents interaction with links and forms when a PNG background image is used. This is sometimes reported as a z-index issue.

    Often this can be solved by giving the links or forms elements hasLayout using position: relative; where possible.

Related Posts

Mar 08
Specifics On Floated Elements

  • A left-floated box will shit to the left until its leftmost margin edge (or border edge if margins are absent) touches either the edge of the containing block, or the edge of another floated box.
  • If the size of the floated box exceeds the available horizontal space, the floated box will be shifted down.
  • Non-positioned, non-floated, block-level elements act as if the floated element is not there, since the floated element is out of document flow.
  • Margins of floated boxes do not collapse with margins of adjacent boxes.
  • The root element (<html>) cannot be floated.
  • An inline element that is floated is converted to a block-level element.
Clear The Floats

The container div itself has no height and no float, since it only contains floating elements. Therefore the background color and border stubbornly stays at the top of the floating columns.

Clear Tthe Floats Bug

Soultion One: Float The Container

					<div>
						<div style="float:left;width:30%;"><p>Some content</p></div>
						<p>Text not inside the float</p>
						<div style="clear:both;"></div>
					</div>
				

Soultion Two: Add Extra Markup

					<div>
						<div style="float:left;width:30%;"><p>Some content</p></div>
						<p>Text not inside the float</p>
						<div class="clearfix"></div>
					</div>
				
					.clearfix {
						clear: both;
					}
				

You could also do this with by means of a <br /> tag with an inline style. In any case, you will have the desired result: the parent container will expand to enclose all of its children. But this method is not recommended since it adds nonsemantic code to your markup.

Soultion Three: The :after Pseudo-Element

The CSS class clearfix is applied to any container that has floating children and does not expand to enclose them.

					.clearfix:after {
						content: ".";
						display: block;
						height: 0;
						clear: both;
						visibility: hidden;
					}
				

But this method does not work in Internet Explorer up to version 7, so an IE-only style needs to be set with one of the following rules:

					.clearfix {
						display: inline-block;
					}
					.clearfix {
						zoom: 1;
					}
				

Depending on the type of issue you are dealing with, one of the above two solutions will resolve the issue in Internet Explorer. It should be noted that the zoom property is a non-standard Microsoft proprietary property, and will case your CSS to become invalid.

Soultion Four: The Overflow Property

By far the best, and easiest solution to reslove the collapsing parent issue is to add overflow: hidden or overflow: auto to the parent element. However, this does not work in IE6. But, in many cases, the parent container will have a set width, which fixes the issue in IE6. If the parent container does not have a width, you can add an IE6-only style with the following code:

					This fix is for IE6 only
					*html .clearfix {
						height: 1%;
						overflow: visible;
					}
				

In IE6, the height property is incorrectly treated as min-height, so this forces the container to enclose its children. The overflow property is then set back to visible, to ensure the content is not hidden or scrolled.

The only drawback to using the overflow method (in any browser) is the possibility that the containing element will have scrollbars or hide content. If there are any elements with negative margins or absolute positioning inside the parent, the will be obscured if they go beyond the parent’s borders, so this method should be used carefully. It should also be noted that if the containing element has to have a specified height or min-height, then you would definitely not be able to use the overflow method.

Related Posts

Mar 07
Conditional Comments

  • Syntax
  •           				<!--[if condition]> HTML <![endif]-->
              			
  • Condition
  •           				<!-- Any Version of IE (typically 5, 5.5, 6, or 7) -->
              				<!--[if IE]> HTML <![endif]-->
              			
              				<!-- Versions Less Than Version -->
              				<!--[if lt IE version]> HTML <![endif]-->
              			
              				<!-- Versions Less Than or Equal To Version -->
              				<!--[if lte IE version]> HTML <![endif]-->
              			
              				<!-- Only Version -->
              				<!--[if IE version]> HTML <![endif]-->
              			
              				<!-- Versions Greater Than or Equal To Version -->
              				<!--[if gte IE version]> HTML <![endif]-->
              			
              				<!-- Versions Greater Than Version -->
              				<!--[if gt IE version]> HTML <![endif]-->
              			
Easy Selectors

  • IE6 and below
  •           				*html .hackname { property: value; }
              			
  • IE7 and below
  •           				*:first-child+html .hackname { property: value; }
              			
              				*html .hackname { property: value; }
              			
  • IE7 only
  •           				*:first-child+html .hackname { property: value; }
              			
  • IE7, Firefox, Safari, and Opera
  •           				html>body .hackname { property: value; }
              			
  • IE8, Firefox, Safari, and Opera (Everything but IE6 and IE7)
  •           				html>/**/body .hackname { property: value; }
              			
  • Firefox 2 only
  •           				.hackname, x:-moz-any-link { property: value; }
              			
  • Firefox 3.0 and newer
  •           				.hackname, x:-moz-any-link, x-default { property: value; }
              			
  • Safari only
  •           				.hackname empty { property: value !important; }
              			
  • Safari 2 – 3.1
  •           				html[xmlns*=""]:root .hackname { property: value; }
              			
  • Safari 2 – 3
  •           				html[xmlns*=""] body:last-child .hackname { property: value; }
              			
  • Opera 9.27 and below, Safari 2
  •           				html:first-child .hackname { property: value; }
              			
  • Safari 2 – 3.1, and Opera 9.25
  •           				*|html[xmlns*=""] .hackname { property: value; }
              			
  • Firefox 3.5+, Safari 3+, Chrome 1+, and Opera 9+
  •           				body:first-of-type .hackname { property: value; }
              			
  • Safari 3+, and Chrome 1+
  •           				@media screen and (-webkit-min-device-pixel-ratio:0) {
              					.hackname { property: value; }
              				}
              			
  • iPhone/Mobile Webkit
  •           				@media screen and (max-device-width: 480px) {
              					.hackname { property: value; }
              				}
              			
  • Everything but IE6 – 8
  •           				:root *> .hackname { property: value; }
              			
Unrecommended Hacks

  • !important
  •           				.hackname {
              					property: value !important; /* IE7 and Modern Browsers */
              					property: value; /* IE6 and below */
              				}
              			
  • _property: value and -property: value
  •           				.hackname {
              					_property: value; /* IE6 and below */
              				}
              			
  • *property: value
  •           				.hackname {
              					*property: value; /* IE7 and below */
              				}
              			
  • property/*\**/: value\9
  •           				.hackname {
              					property/*\**/: value\9; /* IE7 and IE8 */
              				}
              			
  • property: value\9
  •           				.hackname {
              					property: value\9; /* IE6, IE7, and IE8 */
              				}
              			
  • Everything but IE6
  •           				.hackname { property/**/: value; }
              			
Related Posts