JavaScript for Visualforce Developers

But you can if you heed this advice!

Introduction

Take some time and learn JavaScript

(function example(){
alert('In example!');
}());
example();

Separate JavaScript from HTML

<input type="text" id="firstname" onchange="copyToLastname()" />
document.getElementById('firstname').onchange = copyToLastName;

Avoid Visualforce merge syntax in JavaScript

var firstname=’{!Contact.FirstName}’;

Avoid anonymous functions

doRequest(function(response){
console.log('Got response ' + response);
});
doRequest(function(response){
console.log('Got response ' + response);
if (response=='OK') {
doSecondRequest(response, function(nextResponse){
console.log('Got response ' + nextResponse);
});
}
else {
handleError(response, function(okStatus) {
console.log('Handled error okay');
}),
function(errorStatus) {
console.log('Unable to handle error');
}
);
}
});

Don’t pollute the global namespace

var x=1;
var y=2;
var length=3;
var count=7;
window.x=1;
window.y=2;
window.length=3;
window.count=7
var count='This is the counter';
var bgAppObj={
x : 1,
y : 2,
length : 3,
count : 7
};
console.log('Length = ' + bgAppObj.length);
bgAppObj.count+=3;

That’s all there is to it?

CTO at BrightGen, author Visualforce Development Cookbook, multi Salesforce Developer MVP. Salesforce Certified Technical Architect. I am the one who codes.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store