Categories
JavaScript Answers

How to cache an image in JavaScript?

Sometimes, we want to cache an image in JavaScript.

In this article, we’ll look at how to cache an image in JavaScript.

How to cache an image in JavaScript?

To cache an image in JavaScript, we can use the lazysizes library.

For instance, we write

<script src="lazysizes.min.js" async></script>

to add the script tag for the library.

Then we add

<img data-src="images/flower3.png" class="lazyload" alt="" />

to add an img element with the lazyload class.

This will make the lazysizes library cache the image when loaded.

Conclusion

To cache an image in JavaScript, we can use the lazysizes library.

Categories
JavaScript Answers

How to make multiple left-hand assignments with JavaScript?

Sometimes, we want to make multiple left-hand assignments with JavaScript.

In this article, we’ll look at how to make multiple left-hand assignments with JavaScript.

How to make multiple left-hand assignments with JavaScript?

To make multiple left-hand assignments with JavaScript, we first declare all the variables.

And then we can chain the assignments with =.

For instance, we write

let v1, v2, v3;
v1 = v2 = v3 = 200;

to define the v1, v2 and v3 variables with let.

Then we assign 200 to v3.

Then we assign v3 to v2.

And then we assign v2 to v1 in the last line.

Using let v1, v2, v3; lets us make sure that all the variables are block scoped.

Conclusion

To make multiple left-hand assignments with JavaScript, we first declare all the variables.

Categories
JavaScript Answers

How to get visitor’s location using geolocation with JavaScript?

Sometimes, we want to get visitor’s location using geolocation with JavaScript.

In this article, we’ll look at how to get visitor’s location using geolocation with JavaScript.

How to get visitor’s location using geolocation with JavaScript?

To get visitor’s location using geolocation with JavaScript, we can use the ipregistry API.

For instance, we write

const response = await fetch("https://api.ipregistry.co/?key=tryout");
const payload = await response.json();
console.log(payload.location.country.name, payload.location.city);

in an async function to make a GET request to https://api.ipregistry.co/?key=tryout with fetch.

Then we get the location data from the location property of the response.

Conclusion

To get visitor’s location using geolocation with JavaScript, we can use the ipregistry API.

Categories
JavaScript Answers

How to fix ‘CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true’ with Express and JavaScript?

To fix ‘CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true’ with Express and JavaScript, we can change the config of the cors middleware.

For instance, we write

const app = express();
const corsConfig = {
  credentials: true,
  origin: true,
};
app.use(cors(corsConfig));

to set the credentails and origin options to true when we add the cors middleware in our Express app to avoid this error.

Categories
JavaScript Answers

How to get HTTP method in controller with Express.js?

Sometimes, we want to get HTTP method in controller with Express.js and JavaScript.

In this article, we’ll look at how to get HTTP method in controller with Express.js and JavaScript.

How to get HTTP method in controller with Express.js and JavaScript?

To get HTTP method in controller with Express.js and JavaScript, we can use the req.method property.

For instance, we write

const register = (req, res) => {
  if (req.method == "POST") {
    //...
  }
  res.render('user/registration.html', {
    form: form.toHTML()
  });
}

app.get('/register', register)
app.post('/register', register)

to define the register route handler function.

In it, we check the HTTP method used to make the request with req.method.

Then we can use the same route handler function for routes with different methods.

Conclusion

To get HTTP method in controller with Express.js, we can use the req.method property.