Archivio

Posts Tagged ‘javascript’

Leap Year in JavaScript

5 marzo 2010 zaragon Nessun commento

Piccola funzione in JavaScript per verificare se un anno è bisestile.

function isLeapYear(YYYY){
return yyyy % 400 == 0 || (yyyy % 100 != 0 && yyyy % 4 == 0);
}
Categorie:Javascript Tag:

Google Closure Compiler in C#

9 novembre 2009 zaragon Nessun commento

Mads Kristensen has created a C# class for using Google Closure Compiler.

This class is very easy but very useful:

public string Compress(string file)
{
  string source = File.ReadAllText(file);
  XmlDocument xml = CallApi(source);
  return xml.SelectSingleNode("//compiledCode").InnerText;
}

private static XmlDocument CallApi(string source)
{
  using (WebClient client = new WebClient())
  {
    client.Headers.Add("content-type", "application/x-www-form-urlencoded");
    string data = string.Format(PostData, HttpUtility.UrlEncode(source));
    string result = client.UploadString(ApiEndpoint, data);

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(result);
    return doc;
  }
}

For information and for download the complite code, read Mads Kristensen’ s post.

Abount Closure Compiler project.

Categorie:C#, Google Tag: , ,

[Javascript] object.setAttribute IE workaround

18 giugno 2009 zaragon Nessun commento

Non mi era mai capitato prima quindi, a futura memoria, appunto qui il workaround per far funzionare  “object.setAttribute ” su IE:

if(isIE){
   myObject.setAttribute("className", 'myCssClassName');
}
else{
   myObject.setAttribute("class", 'myCssClassName');
}
Categorie:DEV Tag: , ,

[Javascript] getElementsByClassName

18 giugno 2009 zaragon Nessun commento

Volevo condividere con voi questa simpatica funzione javascript; utile se non possiamo usare framework come jQuery o ExtJs

    function getElementsByClassName(classname, tag) {
        if (!tag) tag = "*";
        var anchs = document.getElementsByTagName(tag);
        var total_anchs = anchs.length;
        var regexp = new RegExp('\\b' + classname + '\\b');
        var class_items = new Array()

        for (var i = 0; i < total_anchs; i++) {
            var this_item = anchs[i];
            if (regexp.test(this_item.className)) {
                class_items.push(this_item);
            }
        }
        return class_items;
    }
Categorie:DEV Tag: