Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Thursday, January 26, 2012

Regex using groups in javascript

Convert the xml to uppercase except text or textContent and attribute values.
var XML_String="<start_tag><data>some data</data><data id=\"xyz\" id2=\"yyy\">some data2</data></start_tag>"  
XML_String=XML_String.replace(/(<|<\/)([\"_a-zA-Z0-9\s=]*)(>)/gi, function(s, group1, group2,group3) { 
 if(group2.indexOf("\"")>-1){
  var substr=group2;
  substr=substr.replace(/([a-zA-Z0-9=_\s]*)(\")([_a-zA-Z0-9]*)(\")/gi,function(sb,group1,group2,group3,group4){
   return group1.toUpperCase()+group2+group3+group4;
  });
  group2=substr;   
 }else{
  group2=group2.toUpperCase()
 }   
return group1+group2+group3;});

Sunday, May 22, 2011

Add CDATA in JavaScript

 var newCdata=dataXmlDom.createCDATASection("data to append");  
 newContextElem.appendChild(newCdata);  

Tuesday, April 5, 2011

XSLT on Client

 function xmlTranform(){  
      var xml=file.xml;  
      var xsl=file.xsl;  
      if(window.ActiveXObject)   
      {  
           ex=xml.transformNode(xsl);  
           jDivObj.append(ex);  
      }else if(document.implementation && document.implementation.createDocument){  
           var xsltProcessor=new XSLTProcessor();  
           xsltProcessor.importStylesheet(xsl);  
           var resultDocument=xsltProcessor.transformToFragment(xml,document);  
           var i=resultDocument.firstChild.innerHTML;  
           $("#testDiv").html(i);                                 
      }  
      tb_show("xslt", "#TB_inline?&height=485&width=750&inlineId=testDiv","");// thickbox overlay  
      $("#testDiv").empty();  
 }  

Saturday, March 19, 2011

RegExp in JavaScript

 <script type="text/javascript">  
 var str = "mastertablemastertext";  
 var patt1 = /master/i;  
//i is modifier for case-insensitive and g for Global search
 alert(str.match(patt1));  
 </script>  

Arrays in Javascript

 function start() {  
      var example=new Array();  
      example[0]="first";  
      example[1]="esc";  
      for(var i=0;i<example.length;i++){  
           alert(example[i]);  
      }  
      example[example.length]="third";  
      alert(example.length+" "+example["esc"]);  
 }  

Saturday, January 22, 2011

Parsing using javascript

SAX parser links:
http://code.google.com/p/jssaxparser/

Dom parser links:
http://www.w3schools.com/Dom/dom_parser.asp

Friday, November 26, 2010

Pop-ups

 function post_to_url(path, params, method) {  
      method = method || "post"; // Set method to post by default, if not specified.  
      //Here we are creating a form tag and adding attributes to it   
      var form = document.createElement("form");  
      form.setAttribute("method", method);  
      form.setAttribute("action", path);  
      form.setAttribute("target", "NameofthePopup");   
      //Creating input tag and adding attributes to it  
      for(var key in params) {  
           var hiddenField = document.createElement("input");  
           hiddenField.setAttribute("type", "hidden");  
           hiddenField.setAttribute("name", key);  
           hiddenField.setAttribute("value", params[key]);  
           //adding input tags to from tag.  
           form.appendChild(hiddenField);  
      }  
      document.body.appendChild(form);  // Not entirely sure if this is necessary  
      window.open('URL',"NameofthePopup","width=670,height=680,toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no");  
      form.submit();// submitting the form  
 }  

  • In the above function we are using the Post method to send the parameters.
  • params should be in follwoing formate(inside the flower brackets):{Arg1:'Arg1Value',Arg2:'Arg2Value'} 

Wednesday, November 3, 2010

Char to ascii in javascript

 function ascii_value (c)  
 {  
      // restrict input to a single character  
      c = c . charAt (0);  
      // loop through all possible ASCII values  
      var i;  
      for (i = 0; i < 256; ++ i)  
      {  
           // convert i into a 2-digit hex string  
           var h = i . toString (16);  
           if (h . length == 1)  
                h = "0" + h;  
           // insert a % character into the string  
           h = "%" + h;  
           // determine the character represented by the escape code  
           h = unescape (h);  
           // if the characters match, we've found the ASCII value  
           if (h == c)  
                break;  
      }  
      return i;  
 }  

References:
 Rose, Charlton. "converting between characters and ASCII values." http://sharkysoft.com. 3 Jan 1997. Web. 2 Nov 2010. <http://sharkysoft.com/tutorials/jsa/content/018.html>.