Categories
JavaScript Answers

How to wait until an element is visible with Puppeteer and JavaScript?

Sometimes, we want to wait until an element is visible with Puppeteer and JavaScript.

In this article, we’ll look at how to wait until an element is visible with Puppeteer and JavaScript.

How to wait until an element is visible with Puppeteer and JavaScript?

To wait until an element is visible with Puppeteer and JavaScript, we can use the waitForFunction method.

For instance, we write

await page.waitForFunction(
  "document.querySelector('.btnNext') && document.querySelector('.btnNext').clientHeight !== 0"
);
await page.waitForFunction(
  "document.querySelector('.btnNext') && document.querySelector('.btnNext').style.visibility !== 'hidden'"
);

const btnNext = await page.$(".btnNext");
await btnNext.click();

to call waitForFunction with a string with expressions that checks if the element has a non-zero height and its visibility isn’t hidden.

After that, we get the element with $ and call click to click it.

Conclusion

To wait until an element is visible with Puppeteer and JavaScript, we can use the waitForFunction method.

Categories
React Answers

How to use Google Analytics with a React Next.js app?

Sometimes, we want to use Google Analytics with a React Next.js app.

In this article, we’ll look at how to use Google Analytics with a React Next.js app.

How to use Google Analytics with a React Next.js app?

To use Google Analytics with a React Next.js app, we can use the Script component.

For instance, we write

import Script from "next/script";
import Head from "next/head";

export default function Index() {
  return (
    <>
      <Head>
        <title>Next.js</title>
      </Head>
      <Script
        src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"
        strategy="afterInteractive"
      />
      <Script id="google-analytics" strategy="afterInteractive">
        {`
          window.dataLayer = window.dataLayer || [];
          function gtag(){window.dataLayer.push(arguments);}
          gtag('js', new Date());

          gtag('config', 'GA_MEASUREMENT_ID');
        `}
      </Script>
    </>
  );
}

to add the Script component which has the Google Analytics code.

The Script component as a script tag in the head element.

Conclusion

To use Google Analytics with a React Next.js app, we can use the Script component.

Categories
JavaScript Answers

How to refresh leaflet map when map container is already initialized with JavaScript?

Sometimes, we want to refresh leaflet map when map container is already initialized with JavaScript.

In this article, we’ll look at how to refresh leaflet map when map container is already initialized with JavaScript.

How to refresh leaflet map when map container is already initialized with JavaScript?

To refresh leaflet map when map container is already initialized with JavaScript, we call the off and remove methods before reloading the map.

For instance, we write

map.off();
map.remove();

to clear the map with off and remove before we reload the map to avoid the error.

Conclusion

To refresh leaflet map when map container is already initialized with JavaScript, we call the off and remove methods before reloading the map.

Categories
JavaScript Answers

How to create SVG graphics using JavaScript?

Sometimes, we want to create SVG graphics using JavaScript.

In this article, we’ll look at how to create SVG graphics using JavaScript.

How to create SVG graphics using JavaScript?

To create SVG graphics using JavaScript, we can use the createElementNS and setAttributeNS methods.

For instance, we write

const svgNs = "http://www.w3.org/2000/svg";
const shape = document.createElementNS(svgNs, "circle");
shape.setAttributeNS(null, "cx", 25);
shape.setAttributeNS(null, "cy", 25);
shape.setAttributeNS(null, "r", 20);
shape.setAttributeNS(null, "fill", "green");

to call document.createElementNS to create a circle element with the svgNs namespace.

Then we call shape.setAttributeNS to add the cx, cy, r and fill attributes to the shape circle element and set them to values in the 3rd argument.

Conclusion

To create SVG graphics using JavaScript, we can use the createElementNS and setAttributeNS methods.

Categories
JavaScript Answers

How to add nested JavaScript classes?

Sometimes, we want to add nested JavaScript classes.

In this article, we’ll look at how to add nested JavaScript classes.

How to add nested JavaScript classes?

To add nested JavaScript classes, we can add a class as a static property of another class.

For instance, we write

class A {
  //...
}
A.B = class {
  //...
};

to add the A.B static property and assign it to a class.

Conclusion

To add nested JavaScript classes, we can add a class as a static property of another class.