Open a URL in a new tab (and not a new window)

This is a trick, function openInNewTab(url) { window.open(url, ‘_blank’).focus(); } //or just window.open(url, ‘_blank’).focus(); In most cases, this should happen directly in the onclick handler for the link to prevent pop-up blockers, and the default “new window” behavior. You could do it this way, or by adding an event listener to your DOM object. <div … Read more

How do I refresh a page using JavaScript?

Use location.reload(). For example, to reload whenever an element with id=”something” is clicked: $(‘#something’).click(function() { location.reload(); }); The reload() function takes an optional parameter that can be set to true to force a reload from the server rather than the cache. The parameter defaults to false, so by default the page may reload from the … Read more

How can I get query string values in JavaScript?

Update: Jan-2022 Using Proxy() is faster than using Object.fromEntries() and better supported const params = new Proxy(new URLSearchParams(window.location.search), { get: (searchParams, prop) => searchParams.get(prop), }); // Get the value of “some_key” in eg “https://example.com/?some_key=some_value” let value = params.some_key; // “some_value” Update: June-2021 For a specific case when you need all query params: const urlSearchParams = … Read more

How do I pass command line arguments to a Node.js program?

Standard Method (no library) The arguments are stored in process.argv Here are the node docs on handling command line args: process.argv is an array containing the command line arguments. The first element will be ‘node’, the second element will be the name of the JavaScript file. The next elements will be any additional command line … Read more

How do I get the current date in JavaScript?

Use new Date() to generate a new Date object containing the current date and time. var today = new Date(); var dd = String(today.getDate()).padStart(2, ‘0’); var mm = String(today.getMonth() + 1).padStart(2, ‘0’); //January is 0! var yyyy = today.getFullYear(); today = mm + “https://stackoverflow.com/” + dd + “https://stackoverflow.com/” + yyyy; document.write(today); This will give you … Read more

Length of a JavaScript object

Updated answer Here’s an update as of 2016 and widespread deployment of ES5 and beyond. For IE9+ and all other modern ES5+ capable browsers, you can use Object.keys() so the above code just becomes: var size = Object.keys(myObj).length; This doesn’t have to modify any existing prototype since Object.keys() is now built-in. Edit: Objects can have … Read more

Is there a standard function to check for null, undefined, or blank variables in JavaScript?

You can just check if the variable has a truthy value or not. That means if( value ) { } will evaluate to true if value is not: null undefined NaN empty string (“”) 0 false The above list represents all possible falsy values in ECMA-/Javascript. Find it in the specification at the ToBoolean section. … Read more

error code: 521