Posts

how to check whether element is visible on screen

Sometime we need to load some content when that element is visible on browser. So first we need to find whether that element is visible on the browser or not. Below is the demo example : Javascript function : function isVisible(elem) var TopView = $(window).scrollTop(); var BotView = TopView + $(window).height(); var TopElement = $(elem).offset().top; var BotElement = TopElement + $(elem).height(); return ((BotElement <= BotView) && (TopElement >= TopView)); HTML Part : <div id="my_element" class="no_visible"> <script type="text/javascript"> $(window).scroll(function () if($("#my_element").hasClass("no_visible")) isOnView = isVisible("#my_element"); if(isOnView) $("#my_element").removeClass("no_visible"); ); </script> </div> Use Facebook to Comment on this Post how to check whether element is visible on screen

How to check cookie enabled in browser using javascript

Its quite simple to find the status of the cookies enabled in browser. We can get the status of cookies enabled using below statment: window.navigator.cookieEnabled Use Facebook to Comment on this Post How to check cookie enabled in browser using javascript

Detect Mobile device through javascript

Detect Mobile device through javascript A pretty simple solution is to check for mobile device is the screen width. Almost all mobile devices have a max screen width of 480px. We can get the screen width using below statement: var screen_width = window.screen.availWidth; The only exceptions are tablet pc's like the ipad. These devices have a higher screen width than smartphones. We can use browsers user agent to determine whether this is a mobile device or not. Below is the function to determine the mobile device based on user-agent: (function(a){         if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av

base64 encode using javascript

We can encode any string to a encoded data (using base-64 encoding) string. The btoa() method of window object used to to a encoded data (using base-64 encoding) string. Example : window.btoa('Hello World !!!'); Output : SGVsbG8gV29ybGQgISEh Use Facebook to Comment on this Post base64 encode using javascript

cookie and subdomains

A cookie can be set either for a specific domain or a domain and its subdomain. Cookie for a specific domain: When you set a cookie from a web server in php using setcookie() function then this function accepts some parameters. Some parameters are required and some are optional. In those parameters there is one parameter called domain, which means this cookie would be available for that domain. Domain parameter is an optional parameter. If you don’t set the domain in setcookie() function then it will set the cookie which would be available to current host only. It won’t be available its subdomain. setcookie("TestCookie", $value, time()+3600); Cookie for domain and subdomains: If you set the domain (top level domain) name in setcookie() function then it will set the cookie which would be availabe to its domain and subdomains. For ex. setcookie("TestCookie", $value, time()+3600,'example.com'); Above function will set the cookie with the domain “.example.com”.

PHP Best Practise

Always use PHP standard tags i.e.  <?php ?> .  To ensure the future version support, Please use standard tags for PHP. Always follow a consistent naming standards. Always use Indent and white spaces in codes. It helps to read and understand the code. Always turn on the full error reporting for development. It will show you complete list of notices, warnings and errors. When you are sending you code to production, Please make sure that you have turn off the error reporting else problems would be visible to users.  You can turn on the error reporting by php.ini. error reporting can be enabled on runtime by error_reporting() function. Always do the client and server side validation wherever there is scope of user input. You can not trust on your users. Always put comment in your code. It will make it easy to understand to others. You should use caching mechanism for the performance of the site. Ex.- memcache Don’t do the deep nesting. It will make the code complicated. Always avoid

Output buffering in php

Output buffering is used by PHP to improve performance and to perform a few tricks. Without output buffering (the default), your HTML is sent to the browser in pieces as PHP processes through your script. With output buffering, your HTML is stored in a variable and sent to the browser as one piece at the end of your script. Advantages of output buffering: Turning on output buffering alone decreases the amount of time it takes to download and render our HTML because it’s not being sent to the browser in pieces as PHP processes the HTML. All the fancy stuff we can do with PHP strings, we can now do with our whole HTML page as one variable. If you’ve ever encountered the message “Warning: Cannot modify header information – headers already sent by (output)” while setting cookies, you’ll be happy to know that output buffering is your answer. Use Facebook to Comment on this Post Output buffering in php