stoimen.com/blog

web developing

What is preventing default?

I’m sure you know how to prevent the default action of a link when onclick event is attached to it. Yes, it is a common task and by simply adding a return false; at the end of the method called on click it simply doesn’t call the refresh the page. This is really an everyday task. To describe it first, let’s imagine there’s a link with # href value:

<a onclick="”myFunc()”" href="”#”">click here</a>

Than the definition of myFunc() is something like that:

function myFunc() {
   ...
   return false;
}

If that “return false” part was missing you simply get additional # in the uri of the page and worse – you may be scrolled to the top of the page, because of the lack of an anchor with empty name.

OK, but we know how to deal it! But what happens when you don’t attach click event but mousedown/mouseup pair? It may seem the same thing but it is not!

When you place return false on both event handlers of the mousedown/up events nothing happens, there is a # on the end of the uri and yes the document is scrolled.

How the prevent the default action?

Simply by adding onclick event! We already know that this is working and prevents the default action. Just do the following:

$(‘#element_id’).mousedown(function(){
    ...
    return false;
});
$(#element_id’).mouseup(function() {
   ...
   return false;
});

// this doesn’t work like preventing the default refresh
// action until ....
$(‘#element_id’).click(function() {
   return false;
});
// is added

When I say better documentation I don’t mean it was somehow bad before. The difference is that now the doc pages are version specific, and this is particularly good for those working with older versions of the framework.

In fact most of the cases are just like that, whether because it’s difficult to migrate some code that’s already too different from the original framework itself or because the new version is considered “not so stable”.

Whatever the reason is the version specific docs will be received very well from the community.

  • 0 Comments
  • Filed under: zend framework
  • What are validators?

    In very very breve these are methods which can validate some data, usually user input, against some specific rules. Imagine there’s a web form that is always checked for empty fields or some fields that may contain valid e-mail addresses. This is so common that became everyday routine to almost all of us. However smart developers make abstractions that help them reuse all this functionality. Even smarter developers make use of frameworks. And for those of you, using Zend Framework, there’s no need to write most of the common used validators, simply because they come with the framework itself.

    Technically you’ve various validators in zend, such as Zend_Validate_Alnum, Zend_Validate_Email or Zend_Validate_Regex. All these are extremely useful when it comes to automatic, bullet proof validation, but I’m going to talk more about one specific validator.

    Zend_Validate_Db_RecordExists

    Although the implementation is nothing more than just a chunk of code and doesn’t pretend to be difficult, the idea of such validator is genius indeed. It really helps you do some amazing job.

    Image you have to check some database record existence. Then comes in help this validator. In fact I’m pretty sure almost everyone has experience with such kind of task. Simply because the registration process almost always requires it. When you try to register new user you more often check for the username existence. Although you may solve the problem with other technique by catching the exception the database is throwing for duplicate entries, this should be assumed just as an example.

    I’m pretty sure this validator can be really useful in many occasions!

  • 0 Comments
  • Filed under: zend framework
  • CSS default position value

    position:absolute?

    Recently I was working on a problem where some DOM elements inherited the absolute position from their ancestors. Well this is pretty rare, but however it happens from time to time. If you’re wandering, as I was, how to disable this and to call the default property value just stop hesitating. The default property is static. So you can simply write:

    #element_id {
       position:static;
    }
  • 0 Comments
  • Filed under: css
  • What better example than that coming both from YouTube and Vimeo. Although video tag is not supported except under Safari and Chrome, that’s the future everyone’s going to embed sooner or later.

    And if these “big players” are going that way, don’t hesitate to follow them! HTML5 is really what we as web developers were waiting for so long and even all of the problems during its standardization process it’s really cool there will be tags like video.

    What YouTube is doing today, perhaps will seem normal to everyone tomorrow. However there are some questions still with no answer, as where the flash possibility to add ads into the player is going?! But we’re about to see great things from these two sites!

    Just check out these two links:

    Youtube HTML5

    Vimeo