Categories
JavaScript Answers

How to disable right click on images using jQuery?

Sometimes, we want to disable right click on images using jQuery.

In this article, we’ll look at how to disable right click on images using jQuery.

How to disable right click on images using jQuery?

To disable right click on images using jQuery, we can return false in the contextmenu event handler.

For instance, we write:

<div id='nearestStaticContainer'>
  <img src='https://i.picsum.photos/id/290/200/300.jpg?hmac=kjRyFwJ6i5kuROjzxcs6QbXbBr8EptbH5AuVxtMxhQ0'>
</div>

to add an img element in a div.

Then we write:

$('#nearestStaticContainer').on('contextmenu', 'img', (e) => {
  return false;
});

to select the div with $.

And then we call on with 'contextmenu, 'img' and the contextmenu event handler for the div.

We listen for the contextmenu event emitted on img elements within the div.

And we return false to disable right clicks on the images.

Conclusion

To disable right click on images using jQuery, we can return false in the contextmenu event handler.

Categories
JavaScript Answers

What’s the best practice for embedding arbitrary JSON in the DOM?

Sometimes, we want to embed arbitrary JSON in the DOM.

In this article, we’ll look at what’s the best practice for embedding arbitrary JSON in the DOM.

What’s the best practice for embedding arbitrary JSON in the DOM?

The best practice for embedding arbitrary JSON in the DOM is to put the JSON in a data element.

For instance, we write:

<data class="json-data" value='
  {
    "unicorns": "awesome",
    "abc": [1, 2, 3],
    "careful": "to escape &#39; quotes"
  }
'></data>

to put or JSON data in a data element’s value attribute.

Then we write:

const jsonData = document.querySelector('.json-data');
const data = JSON.parse(jsonData.value);

console.log(data)

to select the data element with document.querySelector.

And then we call JSON.parse to parse the value attribute of the of data element.

Therefore, data is:

{unicorns: 'awesome', abc: Array(3), careful: "to escape ' quotes"}

Conclusion

The best practice for embedding arbitrary JSON in the DOM is to put the JSON in a data element.

Categories
JavaScript Answers

How to use Lodash to find and return an object from an array?

Sometimes, we want to use Lodash to find and return an object from an array.

In this article, we’ll look at how to use Lodash to find and return an object from an array.

How to use Lodash to find and return an object from an array?

To use Lodash to find and return an object from an array, we can use the find method.

For instance, we write:

const songs = [{
    description: 'object1',
    id: 1
  },
  {
    description: 'object2',
    id: 2
  },
  {
    description: 'object3',
    id: 3
  },
  {
    description: 'object4',
    id: 4
  }
]

const id = 3
const song = _.find(songs, {
  id
});
console.log(song)

We call find with the array we’re searching and the property value of each object we’re searching for.

Therefore, we get that song is {description: 'object3', id: 3} since we’re looking for the entry with id 3.

Conclusion

To use Lodash to find and return an object from an array, we can use the find method.

Categories
JavaScript Answers

How to detect support for VML or SVG in a browser with JavaScript?

Sometimes, we want to detect support for VML or SVG in a browser with JavaScript.

In this article, we’ll look at how to detect support for VML or SVG in a browser with JavaScript.

How to detect support for VML or SVG in a browser with JavaScript?

To detect support for VML or SVG in a browser with JavaScript, we can use the document.implementation.hasFeature method.

For instance, we write:

document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Shape", "1.0")

to detect SVG support in the browser.

If it returns true, then it’s supported.

Conclusion

To detect support for VML or SVG in a browser with JavaScript, we can use the document.implementation.hasFeature method.

Categories
JavaScript Answers

How to paginate a JavaScript array?

Sometimes, we want to paginate a JavaScript array.

In this article, we’ll look at how to paginate a JavaScript array.

How to paginate a JavaScript array?

To paginate a JavaScript array, we can use the JavaScript array’s slice method.

For instance, we write:

const paginate = (array, pageSize, pageNumber) => {
  return array.slice((pageNumber - 1) * pageSize, pageNumber * pageSize);
}

console.log(paginate([1, 2, 3, 4, 5, 6], 2, 2));
console.log(paginate([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 4, 1));

to create the paginate function that takes the array we want to pagination and the pageSize and pageNumber.

In the function, we call array.slice to return the elements that should be in the given pageNumber given the pageSize.

We pass in (pageNumber - 1) * pageSize as the start index and pageNumber * pageSize as the end index.

Therefore, from the console log, we get [3, 4] and [1, 2, 3, 4] respectively.

Conclusion

To paginate a JavaScript array, we can use the JavaScript array’s slice method.