Stephen on Software aka SOS

November 13, 2009

Adobe AIR and Json Parsing

Filed under: Uncategorized — Tags: , — sljm @ 2:25 pm

In Adobe AIR, they restrict eval function for all application content for security reasons. So you can’t use eval() to parse a Json string to object, so you will need to go and download the JSON library here.

And just call JSON.parse(string); That should give u back a Javascript object, for you to use.

August 20, 2009

Getting image name from urls, using the Content-Disposition header

Filed under: Programming — Tags: , — sljm @ 1:37 pm

You know when you visit some forums and when you click on a link like this http://thecelebrityspotlight.com/attachment.php?attachmentid=64134&stc=1&thumb=1, they open up an image file and when you right click and save some how the image name appears ? There are no clues in the url that tells you what the image name is suppose to be and somehow magically the browser knows.

Well it appears that the browser is looking for the Content-Disposition header in the Http Response. Jim Ley @ jibbering.com has a page Using the XML HTTP Request Object, that describe how to get the headers using the XmlHttpRequest object.

For Firefox this code snippet should work. Read his blog post for more information.

 var xmlhttp=new XMLHttpRequest();
 xmlhttp.open("HEAD", "/faq/index.html",true);
 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
   alert("Content Disposition: "+
    xmlhttp.getResponseHeader("Content-Disposition"));
  }
 }
 xmlhttp.send(null)

May 18, 2009

Decoding URI/URL strings

Filed under: Programming — Tags: — sljm @ 4:26 pm

In one of my last post, I talking about Finding the Url paramters from a URL using javascript.

But there is a problem with the code, what if the url has some HTML encoding inside like %20 (which represents a space) or some other special html encoding.

You will need to use decodeURI function in javascript. The function will decode html special charecters to their correct form so things like Hello%20World will become Hello World.

function gup( name )
{
 name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
 var regexS = "[\\?&]"+name+"=([^&#]*)";
 var regex = new RegExp( regexS );
 var results = regex.exec( window.location.href );
 if( results == null )
 return "";
 else
 return decodeURI(results[1]);
}

April 23, 2009

Finding Url Parameters using Javascript

Filed under: Programming — Tags: — sljm @ 4:27 pm

Sometimes you want to get the parameters of a url ? In jsp, you can use something like


var name='<%=request.getParameter("name") %>';

if(name==='null'){
    name='';
}

Now I found a much better way using Javascript at this web site . The code is below for reference.

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

The website also has other articles with regards to web programming and javascript.

Blog at WordPress.com.