From the category archives:

AJAX

Javascript Window

A window is essentially just a floating div. We can make the div draggable using the Script.aculo.us Javascript library.

First, we need to create the window div. Here is the CSS I used.


.window {
  background: #d8e7fe;
  position: absolute;
  top: 200px;
  left: 200px;
  width: 350px;
  padding: 1px;
  -moz-border-radius: 7px;
  border: 2px solid #21416d;

}
.window h1 {
  -moz-border-radius-topleft: 5px;
  -moz-border-radius-topright: 5px;
  padding: 6px;
  display: block;
  background-color: #9cc6ff;
  font-size: 13px;
  margin: 0px;
}

And here is the HTML markup.


<div id="mywindow" class="window">
  <h1>Example Window</h1>
  <p>Click and drag this window around the webpage.</p>
</div>

I’ve used ‘-moz-border-radius‘ for rounded corners because I’m lazy. You’ll need to use corner images if you want rounded corners that work in more than one browser.

Now all that’s left to do is the javascript. As I said before, we’ll use the Script.aculo.us library which includes a handy Draggable function. Theres a quick guide on how to install the library here.

Once you’ve got the library installed, add this short Javascript snippet to the head section of your document.


<script type="text/javascript">

function init(){
  new Draggable('mywindow');
}

Event.observe(window, 'load', init, false);

</script>

When the page loads, the init() function is called, which tells the library to make the div ‘mywindow’ draggable!

View the finished product.

{ 1 comment }

Password Strength Meter

July 25, 2007

Making a password strength meter is simple, there’s no tricky Javascript or AJAX needed, just a simple function and a few divs.

Password Strength Meter
Click to view demo.

I’ve decided to design the function around a scoring system, the stronger the password the higher the score. We can accomplish this with just a few simple IF statements.

function passwordStrength(pass)
{

var score = 0;

if(pass.length > 2)
score++;

if(pass.length > 4)
score++;

if(pass.length > 8)
score += 2;

if(/w/.test(pass) && /d/.test(pass))
score++;

return score * 2 * 10;

}

The code is fairly self explanatory, the last IF statement checks if the password contains both characters and symbols. The function returns a percentage of the password’s score out of 5, which is the maximum score it can be awarded.

Next, we need a form with a text box and 2 divs for our meter.


<input type="text" onkeyup="document.getElementById('passmeter').style.width = passwordStrength(this.value) + '%';" />
<br />
Password Strength:<br />
<div id="passmeterbg"><div id="passmeter"></div></div>

The ‘onkeyup’ event calls the passwordStrength() function every time a key is pressed. The ‘width’ property of our meter div is then given the password’s score.

Finally, we need some CSS to style the meter…


#passmeter {
width: 0px;
background: #629d48;
height: 3px;
}

#passmeterbg {
height: 3px;
background: #f1f1f1;
width: 200px;
}

View the finished product.

{ 0 comments }

Use AJAX Sparingly

June 20, 2007

AJAX

AJAX is a only great technique when used in moderation. It belongs in forms and page widgets, but should never be used for navigational purposes.

Call me a traditionalist, but webpages are pages, not desktop applications. Changing the way a UI component works on your website is unnecessary and confusing for your users.

Normal links shouldn’t change page content without warning and pages shouldn’t extend when you reach the bottom.

Use AJAX sparingly if you want to make a better user experience.

{ 0 comments }