Posts

Showing posts with the label javascript

Detect multiple tabs open in vanilla javascript

<script>      // Broadcast that you're opening a page.      localStorage.openpages = Date.now();      window.addEventListener('storage', function (e) {          if (e.key == "openpages" ) {              // Listen if anybody else is opening the same page!              localStorage.page_available = Date.now();          }          if (e.key == "page_available" ) {              alert( "One more page already open" );          }      }, false ); </script> You might need to read the following steps 2 or 3 times to grab the concept properly. When you open the 1 st ...

What are CJS, AMD, UMD, and ESM in Javascript?

  CJS CJS is short for CommonJS. Here is what it looks like: //importing const doSomething = require('./doSomething.js'); //exporting module.exports = function doSomething(n) { // do something } Some of you may immediately recognize CJS syntax from node. That's because node uses CJS module format. CJS imports module synchronously. You can import from a library   node_modules   or local dir. Either by   const myLocalModule = require('./some/local/file.js')   or   var React = require('react');   works. When CJS imports, it will give you a   copy   of the imported object. CJS will not work in the browser. It will have to be transpiled and bundled. AMD AMD stands for Asynchronous Module Definition. Here is a sample code: define(['dep1', 'dep2'], function (dep1, dep2) {     //Define the module value by returning a value.     return function () {}; }); or // "simplified CommonJS wrapping" https://requirejs.org/docs/whyamd.h...