Get ClientContext in SharePoint App

Add the following function

[sourcecode language="javascript"]
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

[/sourcecode]

Then you can use this to get ClientContext

[sourcecode language="javascript"]
siteUrl = getParameterByName("SPAppWebUrl");

var clientContext = new SP.ClientContext(siteUrl);
[/sourcecode]