Categories
JavaScript Answers

How to unzip files with JavaScript?

Sometimes, we want to unzip files with JavaScript.

In this article, we’ll look at how to unzip files with JavaScript.

How to unzip files with JavaScript?

To unzip files with JavaScript, we use the JSZip library.

To install it, we run

npm install jszip

Then we use it by writing

const JSZip = require("jszip");
const zip = new JSZip();
zip.load(file);
zip.files["doc.xml"].asText()

to create the zip JSZip object.

Then we call zip.load to load the file that we get from the file input.

Next, we get the doc.xml file with zip.files["doc.xml"].

And we read it as text with asText.

Conclusion

To unzip files with JavaScript, we use the JSZip library.

Categories
JavaScript Answers

How to redraw or scale Google chart on window resize with JavaScript?

Sometimes, we want to redraw or scale Google chart on window resize with JavaScript.

In this article, we’ll look at how to redraw or scale Google chart on window resize with JavaScript.

How to redraw or scale Google chart on window resize with JavaScript?

To redraw or scale Google chart on window resize with JavaScript, we can redraw the chart when we resize the screen.

For instance, we write

const resize = () => {
  const chart = new google.visualization.LineChart(
    document.getElementById("chart_div")
  );
  chart.draw(data, options);
};

window.onload = resize;
window.onresize = resize;

to create the resize function.

In it, we create a LineChart object with the container element of the chart.

And then we call draw to draw the line chart.

Then we set window.onload and window.onresize to resize to call it when the page loads and when we resize the page respectively.

Conclusion

To redraw or scale Google chart on window resize with JavaScript, we can redraw the chart when we resize the screen.

Categories
JavaScript Answers

How to parse form data request with Express.js and JavaScript?

Sometimes, we want to parse form data request with Express.js and JavaScript.

In this article, we’ll look at how to parse form data request with Express.js and JavaScript.

How to parse form data request with Express.js and JavaScript?

To parse form data request with Express.js and JavaScript, we can use the body-parser package.

To install it, we run

npm i body-parser

Then we write

//...

const bodyParser = require("body-parser");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(bodyParser.urlencoded({ extended: true }));

app.post("/post", (request, response) => {
  console.log(request.body);
  res.send("ok");
});

//...

to add

app.use(bodyParser.urlencoded());
app.use(bodyParser.urlencoded({ extended: true }));

so that we can parse form data request bodies and make them available with request.body in the route handler.

We use request.body in the route handler for the /post route to get the form data request body as an object.

Conclusion

To parse form data request with Express.js and JavaScript, we can use the body-parser package.

Categories
JavaScript Answers

How to get an HTML element’s style values in JavaScript?

Sometimes, we want to get an HTML element’s style values in JavaScript.

In this article, we’ll look at how to get an HTML element’s style values in JavaScript.

How to get an HTML element’s style values in JavaScript?

To get an HTML element’s style values in JavaScript, we can use the getComputedStyle method.

For instance, we write

const width = window.getComputedStyle(document.querySelector("#mainbar")).width;

to call getComputedStyle with the element that we want to get the styles for.

We use querySelector to select the element.

And then we get the computed width of the element with the width property.

Conclusion

To get an HTML element’s style values in JavaScript, we can use the getComputedStyle method.

Categories
JavaScript Answers

How to access elements of parent window from iframe with JavaScript?

Sometimes, we want to access elements of parent window from iframe with JavaScript.

In this article, we’ll look at how to access elements of parent window from iframe with JavaScript.

How to access elements of parent window from iframe with JavaScript?

To access elements of parent window from iframe with JavaScript, we can use the window.parent.document property to get the parent window’s document.

For instance, we write

window.parent.document.getElementById("target");

to use the window.parent.document property to get the parent window’s document.

Then we call getElementById to select the element in the parent window by its ID.

Conclusion

To access elements of parent window from iframe with JavaScript, we can use the window.parent.document property to get the parent window’s document.