Archive

Archive for March, 2009

Javascript Select All/Check All Checkboxes

March 17th, 2009 No comments

In my day-to-day SEO work, there are many sites I use that have a bunch of checkboxes on a single page. For example, in SEO, this is usually to check the box beside websites I want to trade links with. Well this gets tidious, so I went on the hunt for some kind of Javascript, Firefox-addon or mini-program that would do this with 1 click of the mouse. Here’s what I figured out…

At the top of my Firefox, I have a Links bar. I simply created a new link, and added the following code as the URL:

javascript:(function(){ function toggle(box){ temp=box.onchange; box.onchange=null; box.checked=!box.checked; box.onchange=temp; } var x,k,f,j; x=document.forms; for (k=0; k</X.LENGTH;>

I honestly don’t recall where I found this – I’ve been using it for several weeks. But thanks to whoever I found it from. (If I find the site, or you email me, I’ll put the link here.)

Basically what that does is go through each form on a page and toggle the checkboxes on and off. It’s great as a quick-link in my Links bar – I can load the page, click that link (checking all of the check boxes on a page) and click submit. Done!

Categories: Others, Programming Tags: ,

Design Patterns – The Facade Pattern

March 17th, 2009 No comments
What Is The Facade Pattern

The facade pattern is a higher level interface that simplifies and brings together multiple interfaces of a subsystem. What this means is if you want to do something but that requires interacting with multiple subsystems you can create a facade that same only a few methods that handle all the interaction with the subsystem.

Our Application Requirements

In our city dog registration application lets assume there are a few things that need to be done when a new dog is registered. First the new dog and it’s owners must be entered into the system. Next the registration fee must be processed. Finally, we want the system to send the owners the welcome email.

This is a very simple example but this action requires 3 separate systems to do something in order to complete this one task of registering a new dog.

Using The Facade Pattern

For the sake of simplicity and not cluttering this post with too much code, I am not going to provide code for the sub systems, just the facade.

public class RegistrationManager : IRegister { private IAccountingService _accounting; private IMessageService _messaging; private IRepository = _repository;

public RegistrationManager(IAccountService accounting, IMessagingService messaging, IRepository repository) { _accounting = accounting; _messaging = messaging; _repository = repository; }

public void RegisterDog(IDog dog) { _repository.AddDog(dog); _accounting.ProcessPayment(dog.PaymentOrder); _messaging.SendWelcome(dog.Owners.Find(x => x.PrimaryContact)) } }

As you can see this is a very simple example but it illustrates the concept of the pattern. We have taken 3 tasks, each belonging to a different sub system, and combined them into 1 task in a facade class, in this case the RegistrationManager class.

The RegisterDog method adds the new dog to the repository, sends the payment order to the accounting system, and sends a welcome message to the dogs owners that are flagged as primary contacts.

Summing It Up

I hope this post helps you understand the Facade pattern and I hope you are continuing to learn and have fun with this series.

Be sure to grab the RSS feed or follow me on Twitter to stay up to date and not miss any posts.

The C# Design Patterns Series

Part 1 – An Overview

Part 2 – The Decorator Pattern

Part 3 – The Abstract Factory Pattern

Part 4 – The Observer Pattern

Part 5 – The Facade Pattern

2 Responses to “C# Design Patterns – The Facade Pattern”
  1. HardCode Says:

    March 10th, 2009 at 9:27 am

    I just starting to use this pattern with the same goals in mind. It’s good to see it here, confirming my suspicions of how the facade works. Thanks.

  2. BigJim Says:

    March 10th, 2009 at 4:50 pm

    Hey, thanks much for this excellent series. I look forward to future posts on the various patterns. One thing that is confusing to me, I get this pattern and the “command” pattern mixed up. So when you get to the command pattern, maybe you could contrast it with the facade and explain which tasks each is best suited for.

    But thanks again!

Internet Explorer 8 Release Candidate 1 Ready

March 17th, 2009 No comments

A near-final version of Microsoft’s Internet Explorer 8 was made available for download today. The free browser is only available for users of Windows Vista, Windows Server and Windows XP with Service Pack 2 installed. Microsoft has been steadily improving Internet Explorer 8 which has been in development for a few years with Beta 1 and 2 versions of the browser being released in March and August last year respectively.

 

iternet-explorer

 

Although still the most dominant web browser Internet Explorer has seen it’s market share slowly being chipped away by Mozilla Foundation’s Firefox web browser and with Google’s recent and swift entry into the market with Chrome.

Google.com Number 1 According to Alexa

March 17th, 2009 No comments

Alexa.com is a well known web traffic statistics website. It tracks websites visited by persons who have opted to install an Alexa toolbar in Microsoft’s Internet Explorer or Sparky plugin in Mozilla’s Firefox. Alexa’s ranking has been criticized over the years for being unreliable but many website owners and advertisers still use it as a measuring stick to rank the popularity of a website.

For many years Yahoo.com was the most trafficked website in the world according to Alexa but recently Yahoo.com has been overtaken my Google.com as the most trafficked website in the world at least for web users with the Alexa toolbar or sparky plugin installed.

 

alexa

 

Top 10 Sites according to Alexa

Google.com

Yahoo.com

Youtube.com

Live.com

Facebook.com

MSN.com

Wikipedia.org

Myspace.com

Yahoo.co.jp

Categories: Others, Webmasters Resources Tags: ,

How Ajax Works

March 17th, 2009 No comments

In traditional JavaScript coding, if you want to get any information from a database or a file on the server, or send user information to a server, you will have to make an HTML form and GET or POST data to the server. The user will have to click the “Submit” button to send/get the information, wait for the server to respond, and then a new page will load with the results.

Because the server returns a new page each time the user submits input, traditional web applications can run slowly and tend to be less user-friendly. With AJAX, your JavaScript communicates directly with the server, through the JavaScript XMLHttpRequest object.

With an HTTP request, a web page can make a request to, and get a response from a web server, without reloading the page. The user will stay on the same page, and he or she will not notice that scripts request pages, or send data to a server in the background.

This picture is a simplified introduction about how Ajax works:

The user sends a request that executes an action and the action’s response is showed into a layer, identify by an ID, without reload the full page. For example a layer with this id:

<div id=”ajaxResponse”></div>

In the next steps we will see how to create an XMLHttpRequest and receive response from the server.

1. Create XMLhttpRequest

Different browsers use different methods to create the XMLHttpRequest object. Internet Explorer uses an ActiveXObject, while other browsers use the built-in JavaScript object called XMLHttpRequest.

To create this object, and deal with different browsers, we are going to use a “try and catch” statement.

function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject(”Msxml2.XMLHTTP”);
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject(”Microsoft.XMLHTTP”);
}
catch (e)
{
alert(”Your browser does not support AJAX!”);
return false;
}
}
}

2. Sending request to the server

To send off a request to the server, we use the open() method and the send() method.

The open() method takes three arguments. The first argument defines which method to use when sending the request (GET or POST). The second argument specifies the URL of the server-side script. The third argument specifies that the request should be handled asynchronously. The send() method sends the request off to the server.

xmlHttp.open(”GET”,”time.asp”,true);
xmlHttp.send(null);

3. Writing server side script

The responseText will store the data returned from the server. Here we want to send back the current time. The code in “time.asp” looks like this:

<%
response.expires=-1
response.write(time)
%>

4. Consuming the response

Now we need to consume the response received and display it to the user.

xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.myForm.time.value=xmlHttp.responseText;
}
}
xmlHttp.open(”GET”,”time.asp”,true);
xmlHttp.send(null);
}

5. Complete the code

Now we must decide when the AJAX function should be executed. We will let the function run “behind the scenes” when the user types something in the username text field. The complete code looks like this:

<html>
<body>

<script type=”text/javascript”>
function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject(”Msxml2.XMLHTTP”);
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject(”Microsoft.XMLHTTP”);
}
catch (e)
{
alert(”Your browser does not support AJAX!”);
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.myForm.time.value=xmlHttp.responseText;
}
}
xmlHttp.open(”GET”,”time.asp”,true);
xmlHttp.send(null);
}
</script>
<form name=”myForm”>
Name: <input type=”text”
onkeyup=”ajaxFunction();” name=”username” />
Time: <input type=”text” name=”time” />
</form>
</body>
</html>

Categories: Programming Tags: ,

Hello world!

March 3rd, 2009 No comments

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!