Showing entries with tag "Javascript".

Found 6 entries

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");
Leave A Reply

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);
});
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);
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!

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.

Leave A Reply - 11 Replies

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.

Leave A Reply