function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DannyK89DannyK89 

JavaScript Optimization

I have some javascript and I was wondering if anyone could tell me a good way to optimize the code. I am checking the length of a string in the javascript and reducing the number of characters if it goes over a certain number. At the moment the visualforce page that the javascript is on will lag if the character count for the string goes over 2500 or so. This however only happens on Internet Explorer. Every other brower works fine. Is this a problem with the browser or is it a problem with my code. Thanks.

 

function textCounter(field, cntfield, fieldName) {

    var strSpcCounter = 0;
    for(var i = 0; i < document.getElementById(field).value.length; i++){
        var strChar = document.getElementById(field).value.substring(i, i + 1);
    
        if(strChar == '\n'){

            strSpcCounter ++;
        }
    }
    InterviewFormController.getLimit(fieldName, function(result, event){
    if ((document.getElementById(field).value.length + strSpcCounter)  > result) // if too long...trim it!
    document.getElementById(field).value = document.getElementById(field).value.substring(0, (result - strSpcCounter));
// otherwise, update 'characters left' counter
    else
    document.getElementById(cntfield).value = 'You have ' + (result - (document.getElementById(field).value.length + strSpcCounter)) + ' Characters Left';
    document.getElementById(cntfield).align = 'middle';
    }, {escape:true});
}

 

J&A_DevJ&A_Dev

Hi Danny,

 

Rather than relying on JS to check string length, you could use a combination of the LEN(object.field) and LEFT(object.field, 2500) functions on your Visualforce page.

 

Hope that helps.

 

J&A