Wednesday, 3 May 2017

Nodejs boolean initialization

I am trying to make a field visible/invisible depends on a boolean value. The code is being compiled before on the 'catch' part before I take any action on the page. How can I make sure it will run only in a case that the catch block will run?

default.ejs

<textarea id="myTextArea" cols=50 rows=10></textarea>
<button onclick="prettyPrint()">Pretty Print</button>


<script>
    function prettyPrint() {
        try {
            var ugly = document.getElementById('myTextArea').value;
            var leftBracket = '"{';
            var rightBracket = '}"';
            ugly = ugly.replace(new RegExp(leftBracket, "g"), "{")
            ugly = ugly.replace(new RegExp(rightBracket, "g"), "}")

            var obj = JSON.parse(ugly);
            var pretty = JSON.stringify(obj, undefined, 4);
            document.getElementById('myTextArea').value = pretty;
            console.log(pretty);
            document.getElementById("myTextArea").style.borderColor = "blue";
            <% jsonValid = true %>

        } catch (err) {
            console.log(err);
            document.getElementById("myTextArea").style.borderColor = "red";
            <% jsonValid = false %>

        }
    }
</script>



<% if (jsonValid) { %>
    <h2>"json Is Valid"</h2>
    <% } %>

index.js

exports.index = function(req, res) {
    res.render('default', {
        title: 'Home',
        jsonValid: true
    });
}


via Yon E

No comments:

Post a Comment