• 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

August 3rd, 2025 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.

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook

Related

Facebook Twitter Google+ LinkedIn Pinterest
Next article Choosing the Right PHP Framework for Your Web Application: A Comprehensive Guide
Previous article Stripe Payment Gateway Charge Credit Card with PHP Example

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

Cool New JavaScript Features JavaScript
August 5th, 2025

Cool New JavaScript Features

Drag and drop multiple file upload using jQuery, Ajax, and PHP JavaScript
August 4th, 2025

Drag and drop multiple file upload using jQuery, Ajax, and PHP

Convert HTML to PDF in Javascript using jsPDF with Example Download HTML
August 4th, 2025

Convert HTML to PDF in Javascript using jsPDF with Example Download

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe
Get new posts by email:
Powered by follow.it
Advertisement
Like us
Recent Posts
  • Cool New JavaScript Features
  • WhatsApp chatbot in Python using Dialogflow.com
  • Cool CSS3 features
  • PHP Contact Form with Google reCAPTCHA V3 Example
  • Brief overview of some basic HTML tags
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
  • MYSQL
  • PHP framework
  • css
  • jQuery
  • HTML to PDF
  • Web Development
  • About
  • Privacy Policy
  • Back to top
© PHPLift.net. All rights reserved.