Categories
JavaScript Answers

How to download data URL file with JavaScript?

Sometimes, we want to download data URL file with JavaScript.

In this article, we’ll look at how to download data URL file with JavaScript.

How to download data URL file with JavaScript?

To download data URL file with JavaScript, we can create an anchor element that links to the file.

And then we call click to click the link.

For instance, we write

const download = (dataUrl, filename) => {
  const link = document.createElement("a");
  link.href = dataUrl;
  link.download = filename;
  link.click();
};

download("data:text/html,HelloWorld!", "helloWorld.txt");

to create the download function that creates an anchor element with createElement.

Then we set its href attribute to dataUrl.

And we set the download attribute to the file name of the downloaded file.

Then we call click to download the file.

Conclusion

To download data URL file with JavaScript, we can create an anchor element that links to the file.

And then we call click to click the link.

Categories
JavaScript Answers

How to add a fixed footer with React Native?

Sometimes, we want to add a fixed footer with React Native.

In this article, we’ll look at how to add a fixed footer with React Native.

How to add a fixed footer with React Native?

To add a fixed footer with React Native, we can use the ScrollView.

For instance, we write

<View style={{ flex: 1 }}>
  <ScrollView>main</ScrollView>
  <View>
    <Text>footer</Text>
  </View>
</View>

to add a ScrollView that lets us scroll through overflow content.

And we add the footer with

<View>
  <Text>footer</Text>
</View>

We set flex to 1 in the style prop of the outer View to make the flex direction vertical so the items inside will be arranged vertically.

Conclusion

To add a fixed footer with React Native, we can use the ScrollView.

Categories
JavaScript Answers

How to stop a JavaScript for loop?

Sometimes, we want to stop a JavaScript for loop.

In this article, we’ll look at how to stop a JavaScript for loop.

How to stop a JavaScript for loop?

To stop a JavaScript for loop, we can use the break keyword.

For instance, we write

let arr = [1, 2, 3, 4, 5];
for (let ele of arr) {
  if (ele > 3) {
    break;
  }
  console.log(ele);
}

to add a for of loop to loop through the items in the arr array.

In it, we check if the ele variable, which is the item being looped through, is bigger than 3.

If it is, we use break to stop the loop.

Conclusion

To stop a JavaScript for loop, we can use the break keyword.

Categories
JavaScript Answers

How to keep Vuex state on page refresh with Vue.js?

Sometimes, we want to keep Vuex state on page refresh with Vue.js.

In this article, we’ll look at how to keep Vuex state on page refresh with Vue.js.

How to keep Vuex state on page refresh with Vue.js?

To keep Vuex state on page refresh with Vue.js, we use the vuex-persistedstate package.

To install it, we run

npm install --save vuex-persistedstate

Then we use it by writing

import Vue from "vue";
import Vuex from "vuex";
import createPersistedState from "vuex-persistedstate";

Vue.use(Vuex);

export default new Vuex.Store({
  plugins: [
    createPersistedState({
      storage: window.sessionStorage,
    }),
  ],
  state: {
    //....
  },
});

We add the createPersistedState plugin to the Vuex store and then set storage to window.sessionStorage to use session storage to save the Vuex store data.

Conclusion

To keep Vuex state on page refresh with Vue.js, we use the vuex-persistedstate package.

Categories
JavaScript Answers

How to control an iframe player that’s already in the HTML with JavaScript?

Sometimes, we want to control an iframe player that’s already in the HTML with JavaScript.

In this article, we’ll look at how to control an iframe player that’s already in the HTML with JavaScript.

How to control an iframe player that’s already in the HTML with JavaScript?

To control an iframe player that’s already in the HTML with JavaScript, we can specify the ID of the iframe in the YT.Player constructor.

For instance, we write

<iframe
  id="player"
  src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com"
  frameborder="0"
></iframe>

to add the YouTube video iframe.

Then we write

let player;

const onPlayerStateChange = () => {
  //...
};

const onYouTubeIframeAPIReady = () => {
  player = new YT.Player("player", {
    events: {
      onStateChange: onPlayerStateChange,
    },
  });
};

to create the YT.Player object with the ID of the iframe as the first argument.

Conclusion

To control an iframe player that’s already in the HTML with JavaScript, we can specify the ID of the iframe in the YT.Player constructor.