Categories
HTML

How to tell Google Translate to not translate a section of a website?

Sometimes, we want to tell Google Translate to not translate a section of a website.

In this article, we’ll look at how to tell Google Translate to not translate a section of a website.

How to tell Google Translate to not translate a section of a website?

To tell Google Translate to not translate a section of a website, we can add the notranslate class to the elements with text that we don’t want to translate.

For instance, we write

Welcome to the <span class="notranslate">cool</span> company website!

to not translate ‘cool’ in the span by adding the notranslate class to the span.

Conclusion

To tell Google Translate to not translate a section of a website, we can add the notranslate class to the elements with text that we don’t want to translate.

Categories
JavaScript Answers

How to get current URL from an iframe with JavaScript?

Sometimes, we want to get current URL from an iframe with JavaScript.

In this article, we’ll look at how to get current URL from an iframe with JavaScript.

How to get current URL from an iframe with JavaScript?

To get current URL from an iframe with JavaScript, we can use the contentWindow.location.href property.

For instance, we write

const url = document.getElementById("iframe_id").contentWindow.location.href;

to select the iframe with

document.getElementById("iframe_id")

Then we get the current URL of the iframe with the contentWindow.location.href property.

Conclusion

To get current URL from an iframe with JavaScript, we can use the contentWindow.location.href property.

Categories
JavaScript Answers

How to check if JavaScript object is JSON?

Sometimes, we want to check if JavaScript object is JSON.

In this article, we’ll look at how to check if JavaScript object is JSON.

How to check if JavaScript object is JSON?

To check if JavaScript object is JSON, we can use the JSON.parse method.

For instance, we write

const isJson = (data) => {
  try {
    const testIfJson = JSON.parse(data);
    if (typeof testIfJson === "object") {
      return true;
    } else {
      return false;
    }
  } catch {
    return false;
  }
};

to call JSON.parse with data to parse the data string.

If its type is 'object', then it’s JSON is it’s not null.

Otherwise, it’s not JSON.

If JSON.parse can’t parse data, an error will be thrown and false is returned since it’s not JSON.

Conclusion

To check if JavaScript object is JSON, we can use the JSON.parse method.

Categories
JavaScript Answers

How to debug FormData.append(“key”, “value”) is not working with JavaScript?

Sometimes, we want to debug FormData.append("key", "value") is not working with JavaScript.

In this article, we’ll look at how to debug FormData.append("key", "value") is not working with JavaScript.

How to debug FormData.append("key", "value") is not working with JavaScript?

To debug FormData.append("key", "value") is not working with JavaScript, we can loop through the values with a loop.

For instance, we write

for (const data of formData) {
  console.log(data);
}

to loop through the entries of the formData FormData object with a for-of loop.

In it, we log the data value.

This works because a FormData object is an iterable object.

Conclusion

To debug FormData.append("key", "value") is not working with JavaScript, we can loop through the values with a loop.

Categories
JavaScript Answers

How to check if a value is not null and not empty string in JavaScript?

Sometimes, we want to check if a value is not null and not empty string in JavaScript.

In this article, we’ll look at how to check if a value is not null and not empty string in JavaScript.

How to check if a value is not null and not empty string in JavaScript?

To check if a value is not null and not empty string in JavaScript, we can use the !== operator.

For instance, we write

if (data !== null && data !== "") {
  // ...
}

to check if data isn’t null and data isn’t an empty string with

data !== null && data !== ""

Conclusion

To check if a value is not null and not empty string in JavaScript, we can use the !== operator.