Adding to localStorage with JavaScript and Jade


Having issues with Jade and the way the data is passed to it when it is rendered.

I am trying to save the data which is in [{key1: "val1", key2: "val2"}, ...}];
format but having issues as it shows up as the result below.


key: xyz value:[{"artist":"Lady Gaga",...

This is the code I am working with on the server-side Node.js which is passing it fine …

res.render('musics', {
    title: 'site',
    result: JSON.stringify(result)

This is the code I am having issues with because of the way I have to call result in jade…

    function local (arr) {
        var i;
        i = "#{result}";
        localStorage.setItem('xyz', i);

The quotes around result are messing it up but without them I get an error for unexpected identifier…

Any suggestions or if it might be better to go an ajax route through Backbone(which is what I am using with the client-side) I am willing to, just to put some pointers out – the data is being scraped and through selections of a form post – so the data comes back after the post and is a on time transfer, so if I did an ajax call it would have to include the post and the get, otherwise i am not sure of how to receive it… maybe res.json(result) on the server side, but then the page needs to render somehow… Open to suggestions. Thanks! Ultimately I want it to go into localStorage without the " around everything.


your jade snippet should look like this then:

script!= "(function() {localStorage.setItem('xyz',JSON.stringify(" +result + ");})();"

by using != you tell jade to not escape the following content, and on the clientside you have to stringify again before puting your data to local storage.

Leave a Reply

Your email address will not be published. Required fields are marked *