Javascript:ScrollTo() problem in IE6 and its Solution

November 17, 2009

Problem:

Recently i faced a problem in scrolling window to a particular position in IE6. For scrolling window the Javascript method scrollTo(xOffset, yOffset) is used. but it does not work at IE6. ex.

window.scrollTo(0,200);


and this wont do anything at IE6.

Solution:
after some googling i found an interesting solution as follows.
use scrollTo from a timer and it will work. ex.

setTimeout('window.scrollTo(0, 200)',1);

Cheer$


KISS Phylosophy

May 25, 2008

All we know the English term KISS 🙂

But in case of software development or engineering it has special meaning which is a prerequisite to make robust software.

It is widely used as the following variant.

  • Keep It Simple, Stupid
  • Keep It Sweet & Simple
  • Keep It Short & Simple

The main these behind these is to avoid complexity, always try to make things simple , no simpler so that anyone (related with that development) can understand whats going on

There are also other terms like OAOO (One and Only Once) and DRY( Dont Repeat Yourself). These are also very important term in software developemnt. In OO (Object Oriented) design we need to reduce duplication as duplication may increase difficulty of change. It also decrease clarity of the system.

For more info follow the following link
KISS
DRY


Static Method Inheritance

December 25, 2007

We know static methods are callable without instantiating objects. But if it is used from explicit class name, php violates inheritance rule by referencing the current class instead of the called class. Moreover if we use ‘self’ to call the method , it will represent the current class.

Lets see the following code

class BaseClass
{
static function dump()
{
echo __CLASS__ . '::dump()';
}
static function callDump()
{
self::dump(); // self represents the current class
}
}


class ChildClass extends BaseClass
{
static function dump()
{
echo __CLASS__. '::dump()';
}
}
ChildClass::callDump();

In the above code , what the inheritance rule says , it says that the output is supposed to ChildClass::dump() but real output is BaseClass::dump(). This is because ‘self’ always represents the current class.

To solve this limitation PHP introduce Late Static Binding (LSB) from PHP version 5.3.0 that can be used to reference the current class. It is represented as ‘static::’. So the above code will be as following


class BaseClass
{
static function dump()
{
echo __CLASS__ . '::dump()';
}


static function callDump()
{
static::dump(); // static refers the called class
}
}

class ChildClass extends BaseClass
{
static function dump()
{
echo __CLASS__. '::dump()';
}
}

ChildClass::callDump();

Now the above code will output ChildClass::dump(), but ‘static::’ is available from PHP version 5.3.0

For more info please visit the following links
Static Keyworkd
Late Static Binding

Thank$
$haymoL
eXpLoRinG PHP


Custom 404 not found and so on…

December 18, 2007

We are familiar with 404 not found error. it happens when the requested file is not found. But we can customize this error message.

There are some other apache error codes. We can categorize few of them as follows

  • Client Request Errors
    • 400 – Bad Syntax
    • 401 – Authorization Required
    • 404 – Not Found – most common
  • Server Request Errors
    • 500 – Internal Server Error

To customize the error message we need to follow the following steps

  • First, create the HTML page (e404.php) that we want to use as your error message and save it into the home directory
  • Add the following line in the .htaccess file
    • ErrorDocument 404 /e404.php
  • These two files are stored in the home directory (/htdocs/e404.php, /htdocs/.htaccess)
  • Now lets test it . From the browser address bar request (http://localhost/invalidScriptName) for any script that are not eixsts physically and the custom messages will be shown that are used in the e404.php

Thanks
$haymoL
eXploRinG Apache


PHP as Apache module

December 18, 2007

We can use php.ini directives configuration settings such as error_reporting, display_errors, auto_prepend_file etc from apache configuration files (httd.conf or .htaccess file).

Lets we want to append a config file (ex. init.php) at the beginning of a script(index.php). So we need to add the following statement in our .htaccess file.


1. init.php sample file
<?
define("DOCUMENT_ROOT" , $_SERVER['DOCUMENT_ROOT']);
define("DISPLAY_ERROR" , 1);
?>


2. index.php sample file
<?
print_r(DOCUMENT_ROOT);
?>


3..htaccess file
#php_value name value
#Can be used only with PHP_INI_ALL and PHP_INI_PERDIR type directives.

php_value auto_prepend_file init.php

N.B. All the files needs to should be store in the same directory as we use htaccess which used as per directory level. So all the php scripts in that directory will prepend the init.php file.

Now we are ready to test the script.

There are lots of php ini directives that can be altered from apache module.

For more info please visit the php manual

Cheer$
$haymoL
eXplOrinG PHP


GNU Wget for downloading files

December 16, 2007

wget is a GNU free software that can be used to retrieve files using HTTP, HTTPS , FTP, FTPS. It can be used as a commend line from terminal as well as from scripts and cron jobs.

By default, Wget is very simple to invoke.
The basic syntax is:
wget [option]... [URL]
wget -r --tries=10 http://example.com/

There are many options you can set such as startup options, login and input file options, download options, directory options etc.

For more info you can visit the following urls:
GNU Wget | GNU Wget Manual


Operator Precedence in PHP

December 16, 2007

The precedence of an operator specifies how “tightly” it binds two expressions together. Lets play with few of them

  1. ‘[‘ is left associative and has higher precedence than assignment operator ‘=’
    function one($str)
    {
    echo "$str";
    return 1;
    }
    $a = array(1,2);
    $a[one("A")] = $a[one("B")] = 1; // outputs AB
  2. Assignment Operators are right associative
    function output(& $b)
    {
    echo( "b value is: ".$b );
    return $b;
    }
    $a = 2;
    $b = 5;
    echo ($a = $b = 1);
    $a = 2;
    $b = 5;
    echo ( "a value is: ".$a = output($b) );
    ?>
    Output is:
    1
    b value is: 5
    a value is: 5
  3. Comparison Operator always returns TRUE/FALSE
    $b = 5;
    $a = ( ( ++$b ) > 5 ); // Pre-increment test
    echo (int)$a; // Output 1
    $b = 5;
    $a = ( ( $b++ ) > 5 ); // Post-increment test
    echo (int)$a; // Output 0
  4. assignment operator has higher precedence than bitwise operator
    ( 9 & 8 == 8 ) ? 'true':'false'; //same as 9 & ( 8 == 8 ) which is true
    ( 8 & 8 == 8 ) ? 'true':'false'; //same as 8 & ( 8 == 8 ) which is false
  5. Logical Operator Precedence : and, or, nor, !, &&, ||
    $a && $b || $c; // equivalent to
    (a and b) or c
    $a AND $b || $c; // equivalent to a and (b or c)
    again
    $a = $b && $c; // equivalent to $a = (
    $b && $c)
    $a = $b AND $c; //
    equivalent to ($a = $b) && $c
  6. The ternary operator is a statement, and that it doesn’t evaluate to a variable, but to the result of a statement
    echo (true?'true':false?'t':'f'); // equivalent to echo ((true ? 'true' : 'false') ? 't' : 'f');

Reference : PHP Manual


URL Manipulation using mod_rewrite

December 16, 2007

What It is
mod_rewrite is frequently called the “Swiss Army Knife” of URL Manipulation. mod_rewrite is used for rewriting and redirecting URLs dynamically, using powerful pattern matching.
The most common use of mod_rewrite is to make ugly URL most attractive. For example , say our URL looks like http://www.example.com/index.php?cmd=login , and we want to show the url in the address bar as http://www.example.com/login. To do this we need to use the following.

# First make the RewriteEngine On
RewriteEngine On
# Translate login to /index.php?cmd=login
RewriteRule ^login$ /index.php?cmd=login [L]

Installing mod_rewrite
Because of its popularity, mod_rewrite is now included with all common Apache distributions. We can verify if your Apache installation has the mod_rewrite module by looking for a file named mod_rewrite.so under the modules folder in our Apache installation directory.
To make sure open up the httpd.conf file in the Apache conf folder and find the following
#LoadModule rewrite_module modules/mod_rewrite.so
The initail # means it commented out , so to install the mod_rewrite remove the # and restart the Apache. Thats all for mod_rewrite installation.

Testing mod_rewrite
We can write rules in httpd.conf file or in .htaccess file to work for per directory basis. .htaccess is used for customised rules for any specific directory.
Lets create .htaccess file in htdocs folder. and a index.php file that basically dump the POST variable.

index.php file goes as follows
// index.php file that basically do nothing only dump the post variable for this example
$cmd = $GET['login'];
if($cmd == 'login')
{
echo " You are in login Page";
}
else
{
echo "Elsewhere";
}
# First make the RewriteEngine On
RewriteEngine On
# Translate login to /index.php?cmd=login
RewriteRule ^login$ /index.php?cmd=login [L]

Now test the above rule using the url : http://www.example.com/login and see the output.

For more info please visit : Apache Mod Rewrite Rule Doc


OOP with JavaScript

December 16, 2007

JavaScript actually implements all objects as nothing but associative arrays. We can test this in the following example. In Javascript objects can be created in many ways as follows.

  • Simple Object Creation
    • The simplest way to create an object is using keyword new Object().
    • var simpleObject = new Object();
      simpleObject.firstName = 'Foo';
      simpleObject.lastName = 'Bar';
      simpleObject.alertName = function()
      {
      alert('My Name is : ' + this.firstName + ' ' + this.lastName);
      }
    • If we call simpleObject.alertName() or simpleObject.[‘alertName’]() it will alert the firstName and lastName using concatenation
  • JSON
    • Basically we use JSON with the Ajax Request. But JSON is actually a core part of Javascript Specification. Lets build the previous example using JSON.
    • var JSONObject =
      {
      firstName : "Foo",
      lastName : "Bar",
      alertName : function()
      {
      alert('My Name is : ' + this.firstName + ' ' + this.lastName);
      }
      };
    • Now if we call JSONObject.alertName() , the same output will be alerted as the previous one.
  • Prototype
    • Every single object in JavaScript has a prototype property associated with it. the way it works is that when you construct a new instance of an object, all the properties and methods defined in the prototype of the object are attached to the new instance at runtime
    • function PrototypeClass()
      {
      this.firstName = 'Foo';
      this.lastName = 'Bar';
      }
      PrototypeClass.prototype.alertName = function()
      {
      alert('My Name is : ' + this.firstName + ' ' + this.lastName);
      }
      var prototypeObject = new PrototypeClass();
      prototypeObject.alertName(); // it will produce the same output as in the previous example.

Reference : “Practical JavaScript™,DOM Scripting and Ajax Projects”


Father of PHP…

December 5, 2007

I like to search with various interesting terms in my firefox search bar. Last night i searched with “father of php” and found the father of PHP 🙂 . It is Rasmus Lerdorf. He created the PHP/FI as a simple set of Perl scripts for tracking accesses to his online resume. He is also a PEAR administrator.

If you dont like to believe me just google with “father of php” and you will get the answer.

For more info you can visit PHP History Page