Different ways to refresh or reload page using jQuery

Nov 28, 2011 Posted by Lara Kannan 0 comments
In this post, I will show you the different ways using which you can reload or refresh the webpage using jQuery. The first method is nothing to do with jQuery. It is a HTML tag which you need to put in the head section of your page and your page will get refreshed automatically after specified interval.

<meta http-equiv="refresh" content="10">

The meta tag with "http-equiv" is used to refresh the page. This attribute tells the browser that this meta tag is sending an HTTP command rather than a standard meta tag.

Refresh is an actual HTTP header used by the web server.The content attribute in the tag is having value in seconds. As per the above code, it is set to 10, which means after 10 seconds your page will get refreshed or reloaded.

You can also use jQuery to refresh/reload the page automatically. See below jQuery code.
function ReloadPage() { 
$(document).ready(function() {
  setTimeout("ReloadPage()", 10000); .
location.reload() will reload the page again. The advantage of location.reload() is that it works with all the major browsers.

If you want to reload the page on click of button, then you can call location.reload() on button click event. See below jQuery code.
$(document).ready(function() {
     $('#btnReload').click(function() {

Hope this post will help you!


Creating Your Own jQuery Custom Selector

Posted by Lara Kannan 1 comments
jQuery supports a large subset of selectors defined by the CSS3 Selectors draft standard. Additionally it also contains some very useful pseudo classes (similar to :first-child, :hover etc).

Due to its extensible framework, the best part is that jQuery lets you create and define your own custom selectors with ease. A full list of jQuery selectors is available on the jQuery site.

In this article, we will learn how to create our own custom selector that identifies all the mailto: links on a page. Let us see some code first:

As you can observe, we have extended jQuery’s selector expressions under the jQuery.expr[':'] object. To learn more about this, visit the jQuery hot Selector engine called sizzle. We have defined a custom selector called :mailToLink to which we are passing an object, which is a collection of links. This selector looks for all anchor elements that contains an href attribute matching mailto.

That’s it. Our selector :mailToLink is ready and here’s how we can use it:

To test this selector, we have used some hyperlinks, out of which one of them is a mailto: link. When you click the button, we use our custom selector to indentify the :mailto link and set it’s color to red.

As you can see, we have extended jQuery’s expression engine with ease!

The entire source code of this article can be downloaded over here. I hope you liked the article and I thank you for viewing it.

Source: www.dotnetcurry.com

Hope this post will help you!


Web Browser Developer Tools vs Layout Engines

Posted by Lara Kannan 0 comments
A layout engine a.k.a web browser engine or browser rendering engine is a component that parses HTML, XML, CSS, Images etc. and renders it on the screen.

A browser developer tool lets you inspect, debug, analyze and diagnose issues with CSS, HTML, Scripts etc. inside your browser.

As a web developer, you must know what layout engines and developer tools your browser uses.

It helps to understand browser rendering and resolve issues when you are developing cross browser apps. Here’s a list of some popular browser developer tools and the layout engines for your reference.

Browseer Name Layout Engine Developer Tools
Chrome WebKit Google Chrome Inspector
FireFox Gecko Firebug
Internet Explorer and WP 7 Trident IE Developer Tools
Konqueror KHTML KIO
Opera (desktop and mobile) Presto Opera DragonFly
Safari (desktop and mobile) WebKit Web Inspector

Hope this information will help you updated!!!!


Best way of using jQuery Selector

Posted by Lara Kannan 0 comments
It is pretty important to understand how to write efficient element selection statement. One has to be very careful while jQuery selector statement. Below are some tips on how to use your jQuery selectors efficiently.

1. Always try to use ID as selector

You can use ID as selector in jQuery. See below jQuery code.
When IDs are used as selector then jQuery internally makes a call to getElementById() method of Java script which directly maps to the element. When Classes are used as selector then jQuery has to do DOM traversal.So when DOM traversal is performed via jQuery takes more time to select elements. In terms of speed and performance, it is best practice to use IDs as selector.

2. Use class selector with tags

You can use CSS classes as selector. For example, to select elements with "myCSSClass" following jQuery code can be used.
As said earlier, when classes are used DOM traversal happens. But there could be a situation where you need to use classes as selector. For better performance, you can use tag name with the class name. See below
Above jQuery code, restricts the search element specific to DIV elements only.

3. Keep your selector simple, don't make it complex

Avoid complex selectors. You should use make your selectors simple, unless required.
$("body .main p#myID em");
Instead of using such a complex selector, we can simplify it. See below.
$("p#myID em");

4.Don't use your selector repeatedly.

See below jQuery code. The selectors are used thrice for 3 different operation.
$("#myID").css("color", "red"); $("#myID").css("font", "Arial"); $("#myID").text("Error occurred!");
The problem with above code is, jQuery has to traverse 3 times as there are 3 different statements.But this can be combined into a single statement.
$("p").css({ "color": "red", "font": "Arial"}).text("Error occurred!");

5.Know how your selectors are executed

Do you know how the selectors are executed? Your last selectors is always executed first. For example, in below jQuery code, jQuery will first find all the elements with class ".myCssClass" and after that it will reject all the other elements which are not in "p#elmID".
$("p#elmID .myCssClass");

Hope this will help you. Happy coding!!!!

Labels: ,