Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have a Edit/Details form which has 4 user related fields. On click of Save, I save the edited fields to local storage (if supported) and display the same values in the Details view.

Below is the code which does that;

  var UserDataObj = {
name:"John",
email:"[email protected]",
phone:"9999999999",
desc:"some description"
}

/**
 * Check HTML5 Local Storage support
 */
function supports_html5_storage() 
{
    try {
        window.localStorage.setItem( 'checkLocalStorage', true );
        return true;
    } catch ( error ) {
        return false;
    };
};

/**
 * Save form data to local storage
 */
function saveFormData()
{
    if (supports_html5_storage())
    {
    $(".formFieldUserData").each(function(){
        UserDataObj[$(this).attr("name")] = $(this).val();
    });

    localStorage.setItem('UserDataObj', JSON.stringify(UserDataObj));   
    }
}


/**
 * Set field values based on local storage
 */
function setFormFieldValues()
{
    if (supports_html5_storage())
    {
            var retrievedUserDataObj = JSON.parse(localStorage.getItem('UserDataObj'));
            if(retrievedUserDataObj)
            {
                $(".formFieldUserData").each(function(){
                    var currentKey = $(this).attr("name");
                    var retrievedValue = retrievedUserDataObj[$(this).attr("name")]
                    $("#"+currentKey).val(retrievedValue);
                    $("#"+currentKey+"Txt").html(retrievedValue);
                });
            }
            else
            {
                $(".formFieldUserData").each(function(){
                    var currentKey = $(this).attr("name");
                    var userDataObjValue = UserDataObj[$(this).attr("name")]
                    $("#"+currentKey).val(userDataObjValue);
                    $("#"+currentKey+"Txt").html(userDataObjValue);
                });
            }
    }
    else
    {
        $(".formFieldUserData").each(function(){
        var currentKey = $(this).attr("name");
            if ($(this).val() != "")
            {
            var fieldValue = $(this).val();
            $("#"+currentKey).val(fieldValue);
            $("#"+currentKey+"Txt").html(fieldValue);
            }   
            else
            {
            var defaultuserDataObjValue = UserDataObj[$(this).attr("name")];
            $("#"+currentKey).val(defaultuserDataObjValue);
            $("#"+currentKey+"Txt").html(defaultuserDataObjValue);
            }
        })
    }
}

My question is; 1. This is a completely working code. But is there any further that I can do as far as best coding practices go. 2. Also performance wise, can I do anything to improve the overall code ?

Thank you.

share|improve this question
2  
Cross posted to Stack Overflow (where it is closed as o/t) and Code Review. Probably a better fit for Code Review, so recommending closing here. Downvoting for undeclared crossposting, as this creates duplicate effort. – halfer Mar 10 at 11:51

closed as off topic by gnat, Martijn Pieters, Glenn Nelson, Martin Wickman, MichaelT Mar 10 at 16:43

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

Javascript does not have block scope, only function scope, so you should declare all of your variables at the beginning of a function using one var statement. Checkout javascript's 'function hoisting' feature.

Also you could run your code through JSlint to improve the style.

share|improve this answer
Please don't declare all your variable at the beginning of the scope, it really decrease readability. However, always keep in mind this remark: javascript's variable's scope is function-wide :) – Clement Herreman Mar 11 at 11:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.