Archive for the ‘Javascript’ category

Virtual namespaces with javascript

September 27th, 2010

In most programming languages relatively mature and powerful, there is what we call a namespace. The namespace is a container name where we hold classes, functions and variables that can be repeated with the same name in other namespaces without involving an error. A example of what the real utility of organizing or namespaces is in the graphics libraries of programming languages, since in these libraries a very common element the Window class is usually referred to a window but may be in GTK, QT,. Net, Windows API, etc. (This is not quite correct since GTK would GtkWindow but for the example). Javascript as a language and more to mature over the years and relatively standardized by modern browsers, despite being a multiparadigm programming language, as do PHP, also has namespaces, but due to weakness of these namespaces variables are more virtual than real.

And understanding that it is serving a namespace to declare it in javascript, what we should do is use arrays to generate. Thanks to the weakness and dynamism of the variables in javascript, we can assign to a variable array of positions by name and access these through a punctuation mark "." and not their common key array ["key"], so here comes the trick. Basically what we do by declaring a namespace in javascript is assigned to a variable array with the positions of the namespace and then and within these positions, log in as if from .Net or Java in question. With an example is simpler.

var Indalcasa = { Tools : {} };
 
Indalcasa.Tools.MyClass = function() {
  this.variable = "value";
 
  this.myMethod = function(parameters) {
    return this.variable + " " + parameters;
  }
};
 
var iClass = new Indalcasa.Tools.MyClass();
iClass.variable = "new value";
alert(iClass.myMethod("test"));

The above example shows an alert window with a message such that "new value test", as we have instantiated the namespace class, we changed the value to the class attribute and the class method, concatenate the value of class attribute to the parameters that we have passed. This is a small and simple example of what can be done with javascript, namespaces and classes.

Via: Namespaces o espacios de nombre virtuales en javascript

Valid Autocomplete=”off” for XHTML

September 12th, 2010

When writing a text in a text input type (<input type="text" />) we find that generally the browser is going to autocomplete what you are writing at that time with text that we have already written earlier, recalling for input called "name" the name or names you have entered.

The practice of auto is very helpful whenever we have repetitive forms such as registration forms are similarly always call and they always ask the same data, avoid rewrite all our data over and over again.

There are cases where you might not want that the browser will autocomplete the text, may be the case that we have an online store that paying by credit card and we do not want that credit card we autocomplete. To avoid this, there is an attribute that is used by the vast majority of browsers is the attribute autocomplete="on|off", if the value is cleared off, tells the browser not autocomplete the field.

Although in many cases it is good practice to use the autocomplete attribute, the W3C has recognized this attribute as an attribute of the input tag with which to try to validate and meet with the autocomplete attribute, we give a validation error. To resolve the validation error that will be using javascript to add this attribute, the functionality is still there, but we will not have validation issues.

The only thing we do is add the following code at the bottom of the page, or at least, just after declaring the input

<script type="text/javascript">
    //<![CDATA[
    var inputElement = document.getElementById("inputId")
    inputElement.setAttribute("autocomplete", "off");
    //]]>
  </script>

In this way we will ensure that when entering text in a input autocomplete not by the browser and if we can do for you autocomplete javascript.

A good and beautiful example of autocomplete fields is the use for muchoviaje in the areas of origin and destination.

Via: Autocomplete=”off” valido para XHTML