Searched for tag Javascript and found 7 results in 0.7 ms

Javascript: Returning data from an AJAX call

Javascript is by it's nature asynchronous, which makes returning data from an AJAX call complicated. Modern Javascript uses the concept of a "promise" to allow you to run code after an asynchronous call competes. This is similar to using a callback function, but it reads cleaner. JQuery implements this type of call with the then() function. A simple example is as follows:

function getData() {
    return $.ajax({
        url: '/api/data'
    });
}

// Usage
getData().then(function(data) {
    console.log(data);
});
Tags:
Leave A Reply

Javascript: Copy string to clipboard

I needed a modern way to copy a string to the clipboard in JavaScript. Claude.ai helped me come up with this:

// With async/await
async function copyToClipboard(text) {
    try {
        await navigator.clipboard.writeText(text);
        console.log('Copied to clipboard');
    } catch (err) {
        console.error('Failed to copy: ', err);
    }
}

Then you simply call it with a string

copyToClipboard("Hello world");
Tags:
Leave A Reply

Javascript: Get a unixtime value

I need a Unixtime in Javascript. This is the simplest way I came up with to get that value:

var unixtime = parseInt(new Date().getTime() / 1000);

Update: Newer versions of Javascript (and browsers) now offer Date.now() which returns milliseconds since the epoch with less typing required.

var unixtime = parseInt(Date.now / 1000);
Tags:
Leave A Reply

Javascript Unixtime converter

I always end up trying to figure out what a unixtime is in human readable format, or vice versa. I ended up borrowing and updating a tool from captain.at, striping out all the ads and other junk and cleaning it up so it's XHTML compliant. Check out my new and improved javascript unixtime converter.

Tags:
Leave A Reply

Javascript: Unixtimes and Leap Seconds

There is going to be a leap second today at 1230768000 (2008 December 31, 23h 59m 60s). I updated my Javascript Unixtime converter to include options for UTC time. Happy Leap Seconds!

Tags:
Leave A Reply

Javascript Unixtime

Just a note of how to get Unixtime in Javascipt.

var date_obj    = new Date;
var unixtime_ms = date_obj.getTime();
var unixtime    = parseInt(unixtime_ms / 1000);

Also a Javascript Unixtime conversion utility.

Tags:
Leave A Reply - 11 Replies

Mustache is a very simple and powerful templating language

I am late to the game learning about the Mustache templating language. It is very mature, and has support for all of the languages I care about: Perl, PHP, and Javascript. I am mainly focused on using it in Javascript as it is a great way to format raw data you get via JSON into something more human consumable like HTML.

It's incredibly easy to implement and get started with. A single include loads the entire zero-dependency library. As of v4.2.0 the library is tiny, clocking in at only 25k. Once loaded Mustache.render(template_str, data_obj) gets you a formatted string. A big selling point is the ease of installation and use. You can be up and running with Mustache.js in less than 5 minutes.

<script src="/js/mustache.min.js"></script>
var data = { name: "Jason", animal: "Kitten" };
var str  = Mustache.render("<p>Hello {{name}}, I hear you like {{animal}}s</p>", data);

I liked it so much I wrote up a Mustache sandbox to allow me to play around with the templating system live. The template syntax is a little archaic, but it is workable.

Note: They even have Arduino support, which I need to test.

Tags:
Leave A Reply