javascript


  • (JS) Calculate price excluding VAT

    Sometimes you need to calculate the price of a product excluding VAT, and the only details you have is the amount including vat and the vat percent. This might be a bit tricky in some applications when there are mixed VAT percentages. For example, you payed 1000 space credits and in that sum there is…

  • (JS) Validate date in format YYYY-MM-DD

    A simple JS function to validate that a date string in the format YYYY-MM-DD is a valid date. Will validate that the day is correct for the given month, including leap years

  • (JS) Validate Swedish personnummer and organisationsnummer

    JavaScript functions for validating Swedish personal identity numbers (personnummer), and organisation numbers (organisationsnummer). The functions for personal identity number will also validate co-ordination number (samordningsnummer).

  • (JS) Use include method in IE

    Here’s a hack for using the includes method also in the horrible Internet Explorer. [js] if (!String.prototype.includes) { String.prototype.includes = function () { ‘use strict’; return String.prototype.indexOf.apply(this, arguments) !== -1; }; } [/js]

  • (JS) Newline to br

    Replaces newline characters with <br> tags in the same style as the php nl2br function

  • (JS) Super simple slider/carousel/slideshow

    Here’s how you can make a super simple slideshow that changes slides on a timer interval. You could easily edit it so any other event, like a mouse click, will trigger the slide() function. You can see it in action HERE HTML [html] <div class="slider"> <div class="slide red"> <p>THIS IS SLIDE 1</p> </div> <div class="slide…

  • (PHP) Log Laravel execution time to console

    Put this in app/start/global.php to get Laravels execution time to the browser console log. L4 [php] $start = microtime(true); App::finish(function() use ($start) { echo &quot;&lt;script&gt;console.log(‘App finish: &quot;.round((microtime(true) – $start) * 1000, 3).&quot; ms’)&lt;/script&gt;&quot;; }); [/php] This works with L5: [html] This page took {{ (microtime(true) – LARAVEL_START) }} seconds to render [/html]

  • Open weblinks in external browser when using Phonegap

    To prevent a Phonegap-wrapped app from opening external links in the apps webview, use javascript to open the link in the external browser. This works on Android (probably other platforms as well) [html]<a href="#" data-rel="external" target="_blank" onclick="window.open(‘http://www.jymden.com’, ‘_system’)">Jymden</a>[/html]

  • Convert seconds to days, hours, minutes and seconds

    Converting seconds into days, hours, minutes and seconds int days = secondsTotal / 60 / 60 / 24; int hours = (secondsTotal / 60 / 60) % 24; int minutes = (secondsTotal / 60) % 60; int seconds = secondsTotal % 60;