8
Some useful JavaScript One-Liners
source link: https://dev.to/shshank/some-useful-javascript-one-liners-4eoe
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Some useful JavaScript One-Liners
JavaScript is one of the most powerful language in this modern world. In this article we will go through some of the useful JavaScript one-liners.
- If you want to get the text that a user selects or highlights on a web page, there is a useful one-liner for that.
const getSelectedText = () => window.getSelection().toString();
console.log(getSelectedText);
- There is a method called
scrollTo(x,y)
, it allows you to scroll to a particular set of used coordinates.
const scrollToTop = () => window.scrollTo(0,0);
- If you want to have a smooth scrolling animation, just do something like this:
const Top = () => window.scrollTo({top:0, behavior: 'smooth'});
- Internet bandwidth is basically the amount of data is being transferred over an internet connection in a specified period of time.
navigator.connection.downlink;
- If you want to redirect user to a specified location, you can do something like this:
const urlRedirect = url => location.href = url;
urlRedirect('https://dev.to/');
Hope you'll find some useful snippets in this code. Comment if you have more other than these.
Happy Coding Devs.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK