Javascript query through sforce

<apex:page >
<apex:includeScript value="/soap/ajax/28.0/connection.js"/>
<apex:includeScript value="/soap/ajax/28.0/apex.js"/>
<script>
sforce.connection.sessionId = "{!$Api.Session_ID}";
function doAnon() {
    result = sforce.connection.query("Select Name, Id from Account LIMIT 5");
  records = result.getArray("records");

  for (var i=0; i< records.length; i++) {
    var record = records[i];
  //  alert(record.Name + " -- " + record.Id);
  document.getElementById("op").innerHTML += record.Name + " -- " + record.Id + "</br>";
  }
}

</script>
<button onclick="doAnon()">Execute</button>
<div id="op"></div>
</apex:page>




Also Refer 

Passing JavaScript values to Apex class

Passing Apex inner class List to JavaScript

Comments