• Home
  • PHP
  • MySQL
  • Laravel
  • Demos
  • HTML
  • jQuery
  • Framework
  • Request Tutorial
PHP Lift
  • Home
  • Demos
  • Advertisement
PHP Lift
  • Home
  • PHP
  • MySQL
  • Laravel
  • Demos
  • HTML
  • jQuery
  • Framework
  • Request Tutorial
  • Follow
    • Facebook
    • Twitter
    • Google+
    • Pinterest
    • Youtube
    • Instagram
    • RSS
Useful JavaScript globals
Home
JavaScript

Useful JavaScript globals

February 4th, 2026 Ivan Dimov JavaScript 0 comments

Facebook Twitter Google+ LinkedIn Pinterest

Math.random() – generates a randon number between 0 and 1. You can multiply the number to get a number in a different range.

Math.floor(numberHere) – it would trim down the decimal part of a float and the end result is always an integer

Math.ceil(numberHere)  – it would give the integer to which a decimal is closest to; Math.ceil(4.6) would result in 5 while Math.ceil(4.4) would result in 4.

window.innerWidth – returns the inner width of a window’s content area

window.innerHeight – returns the inner height of a window’s content area

window.outerHeight – returns the height in pixels of the whole browser window; including any sidebars and browser utilities.

window.outerWidth – returns the width in pixels of the whole browser window; including any sidebars and browser utilities.

window.location – contains information about the current URL and can be used to redirect the user

window,location.href – returns the URL of the current page.

window.location.hostname –  returns the domain name of the web host

window.location.pathname – returns the path and filename of the current page

window.location.protocol – returns the web protocol used (http: or https:)

window.location.assign – loads a new document

window.location.reload() – reloads the current page

window.localStorage – a local storage where you can set data for future use in the user’s browser; you can treat it like an object to which you can assign and delete properties

document.querySelector(“.cssSelector”) – a new JavaScript addition that allows you to use CSS-like selectors without a library such as jQuery. This particular method returns the first matched element.

document.querySelectorAll(“.cssSelector”) – like document.querySelector but returns all matched elements.

window.encodeURI(“http://example.com”) – encodes a URL for different purposes such as the insertion of an URL as a GET parameter

window.decodeURI(“http://example.com”) – decodes an URL to its original state.

document.cookie – the cookie object contains variables that last for a specified duration between individual page and possibly browsing session changes.

parseInt(“12”, 10) – converts a numeric string into an integer. The second argument is the radix.

parseFloat(“40 years”) – converts a numeric string to a float.

41.toString() – casts different data types to string

Event Handlers

Event handlers allows an interaction to happen when the user performs certain activity on the given website.

document/element.onmousedown allows you to hook code that will be executed whenever the user clicks (puts the mouse button down) the left mouse button. Here is how you can use it:

document.onmousedown = function() {



      alert(1);



  }



document.querySelector(“p”).onmousedown = function() {



      alert(1);



  }

element.onmouseenter allows you to set an event listener that will be called whenever the mouse/cursor is within the area of the element.

Element.onmouseleave allows you to set an event listener that will be called when the mouse/cursor leaves the given element.

Element.onkeypress allows you to set an event listener that will be called when a keyboard key is pressed. Inside the event handler you can check which key was pressed to look and react to specific keyboard keys. You can check http://stackoverflow.com/questions/1846599/how-to-find-out-what-character-key-is-pressed to see how it is done in practice.

Facebook Twitter Google+ LinkedIn Pinterest
Next article Cool New JavaScript Features
Previous article How to show Image before upload JavaScript & HTML5 FileReader()

Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter

Related Posts

7 Top Reasons to Learn JavaScript in 2021 Guide
March 19th, 2026

7 Top Reasons to Learn JavaScript in 2021

What's Vanilla JS? Why Vanilla JS? JavaScript
March 19th, 2026

What's Vanilla JS? Why Vanilla JS?

5 JavaScript Libraries You Can Use For Your Next Project JavaScript
March 19th, 2026

5 JavaScript Libraries You Can Use For Your Next Project

Leave a Reply Cancel reply

Subscribe
Get new posts by email:
Powered by follow.it
Advertisement
Like us
Recent Posts
  • Top 7 Websites To Get Your First Paid Internship
  • Laravel Data and Value Objects: Harnessing the Power of Immutability and Consistency
  • Choosing the Right PHP Framework for Your Web Application: A Comprehensive Guide
  • Understanding the Factory Design Pattern in PHP: Simplifying Object Creation
  • 7 Top Reasons to Learn JavaScript in 2021
Categories
  • API
  • Bootstrap
  • Bot
  • CSS
  • CSS 3
  • Database
  • Designing
  • Framework
  • Guide
  • HTML
  • HTML 5
  • JavaScript
  • jQuery
  • Laravel
  • MySQL
  • Node.js
  • oAuth
  • Payment
  • PHP
  • Python
  • Social
  • Tips
  • Web 3.0
  • WordPress
Weekly Tags
  • PHP
  • How to
  • javascript
  • laravel
  • Web Development
  • jQuery
  • MYSQL
  • HTML to PDF
  • PHP framework
  • css
  • About
  • Privacy Policy
  • Back to top
© PHPLift.net. All rights reserved.