Categories
JavaScript Answers

How to Properly Unload or Destroy an HTML Element with JavaScript?

Sometimes, we want to properly unload or destroy an HTML element with JavaScript.

In this article, we’ll look at how to properly unload or destroy an HTML element with JavaScript.

Properly Unload or Destroy an HTML Element with JavaScript

To properly unload or destroy an HTML element with JavaScript, we can pause the video, then remove the src attribute from the video element, and then call load again to reload the video element.

For instance, if we have the following HTML video element:

<video src='https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_480_1_5MG.mp4' controls></video>

We can write:

const videoElement = document.querySelector('video');
videoElement.pause();
videoElement.removeAttribute('src');
videoElement.load();

to get the video element with document.querySelector .

Then we call pause to pause the video.

Next, we call removeAttribute with 'src' to remove the src attribute.

And then we call load to reload the video element.

Conclusion

To properly unload or destroy an HTML element with JavaScript, we can pause the video, then remove the src attribute from the video element, and then call load again to reload the video element.

Categories
JavaScript Answers

How to Disable Related Videos with the YouTube JavaScript API?

Sometimes, we want to disable related videos with the Youtube JavaScript API. In this article, we’ll look at how to disable related videos with the Youtube JavaScript API.

Disable Related Videos with the Youtube JavaScript API

To disable related videos with the Youtube JavaScript API, we can set the rel property in the playerVars property to 0.

For instance, we can add the following HTML element for the video player:

<div id="player"></div>

Then we can write:

const tag = document.createElement("script");
tag.id = "iframe-demo";
tag.src = "https://www.youtube.com/iframe_api";
const [firstScriptTag] = document.getElementsByTagName("script");
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

window.onYouTubeIframeAPIReady = () => {  
  const player = new window.YT.Player("player", {    
    width: 200,    
    height: 100,    
    videoId: "M7lc1UVf-VE",    
    playerVars: {      
      rel: 0,      
      showinfo: 0,      
      ecver: 2   
    }  
  });
};

to add the YouTube script tag with:

const tag = document.createElement("script");
tag.id = "iframe-demo";
tag.src = "https://www.youtube.com/iframe_api";
const [firstScriptTag] = document.getElementsByTagName("script");
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

Then we set the window.onYouTubeIframeAPIReady to a function that invokes the window.YT.Player constructor with the ID of the element for the player as the first argument.

The 2nd argument is an object with various options like the video width and height and video ID. The playerVars.rel property is set to 0 so that related videos will not be shown.

Conclusion

To disable related videos with the Youtube JavaScript API, we can set the rel property in the playerVars property to 0.

Categories
JavaScript Answers

How to Pass Additional Data via Custom Events in JavaScript?

Sometimes, we want to pass additional data via custom events in JavaScript.

In this article, we’ll look at how to pass additional data via custom events in JavaScript.

Pass Additional Data via Custom Events in JavaScript

To pass additional data via custom events in JavaScript, we can pass an object into the CustomEvent constructor.

For instance, we can write:

window.addEventListener("MyEvent", (evt) => {
  console.log(evt.detail);
});

const evt = new CustomEvent("MyEvent", {
  detail: "Any Object Here"
});
window.dispatchEvent(evt);

to listen to the MyEvent event with the window.addEventListener method.

In the event handler, we log the value of the evt.detail property, which will be set when we pass in an object into the CustomEvent constructor’s 2nd argument with the detail property.

The first argument of the CustomEvent constructor is the event name.

Finally, we call window.dispatchEvent with the evt object to emit the event.

Therefore, we should see 'Any Object Here' logged from the event handler.

Conclusion

To pass additional data via custom events in JavaScript, we can pass an object into the CustomEvent constructor.

Categories
JavaScript Answers jQuery

How to Determine the Pixel Length of a String in JavaScript?

Sometimes, we want to determine the pixel length of a string in JavaScript.

In this article, we’ll look at how to determine the pixel length of a string in JavaScript.

Determine the Pixel Length of a String in JavaScript

To determine the pixel length of a string in JavaScript, we can put the string in a span and then use the jQuery width method to determine the width of the span.

For instance, we can write the following HTML:

<span></span>

Then we can write:

const span = document.querySelector('span')  
span.innerText = 'Here is my string'  
console.log($(span).width())

to get the span width document.querySelector .

And then we set innerText to the string of the span.

Finally, we log the span’s width that we get from $(span).width() from jQuery.

The console log should log 111.

Conclusion

To determine the pixel length of a string in JavaScript, we can put the string in a span and then use the jQuery width method to determine the width of the span.

Categories
JavaScript Answers

How to Remove Zero-Width Space Characters from a JavaScript String?

Sometimes, we want to remove zero-width space characters from a JavaScript string.

In this article, we’ll look at how to remove zero-width space characters from a JavaScript string.

Remove Zero-Width Space Characters from a JavaScript String

To remove zero-width space characters from a JavaScript string, we can use the JavaScript string replace method that matches all zero-width characters and replace them with empty strings.

Zero-width characters in Unicode includes:

  • U+200B zero width space
  • U+200C zero-width non-joiner Unicode code point
  • U+200D zero width joiner Unicode code point
  • U+FEFF zero-width no-break space Unicode code point

For instance, we can do the replacement by writing:

const userInput = 'au200Bbu200Ccu200DduFEFFe';
console.log(userInput.length);
const result = userInput.replace(/[u200B-u200DuFEFF]/g, '');
console.log(result.length);

We have the userInput string that has the zero-width characters listed.

From the first console log, we see userInput.length is 9.

Then we call replace with a regex that matches the zero-width characters listed and replaces them all with empty strings.

And so we see that result.length is 5, so the zero-width characters were removed.

Conclusion

To remove zero-width space characters from a JavaScript string, we can use the JavaScript string replace method that matches all zero-width characters and replaces them with empty strings.