stop()
Published Feb 25, 2025Updated Feb 27, 2025
Contribute to Docs
The .stop()
method immediately terminates the loading of content in the current window, including images, animations, and scripts. This provides programmatic control over page loading, similar to manually stopping a page using the browser’s stop button or the Escape key.
Syntax
window.stop()
Example
This JavaScript code demonstrates the usage of .stop()
by creating a simple resource loading interface. The code creates three main elements:
- A “Load Resources” button that starts loading images.
- A “Stop Loading” button that prevents further loading.
- A status display (
div
) to show the loading progress.
const loadButton = document.createElement('button');loadButton.textContent = 'Load Resources';document.body.appendChild(loadButton);// Create a stop buttonconst stopButton = document.createElement('button');stopButton.textContent = 'Stop Loading';document.body.appendChild(stopButton);// Create a status displayconst status = document.createElement('div');status.id = 'status';document.body.appendChild(status);// Function to simulate loading multiple resourcesfunction loadResources() {status.textContent = 'Loading resources...';// Simulate loading multiple heavy resourcesfor (let i = 1; i <= 5; i++) {const img = new Image();img.src = `/api/placeholder/1000/1000?text=Resource${i}&delay=2000`;img.onload = () => {status.textContent = `Loaded resource ${i} of 5`;};img.onerror = () => {status.textContent = `Resource ${i} loading failed (possibly stopped)`;};}}// Add click handlersloadButton.onclick = loadResources;stopButton.onclick = () => {window.stop();status.textContent = 'Loading stopped by user';};
Note: The code must be used within an HTML document since it manipulates the DOM (Document Object Model) and relies on the browser’s window object.
When the “Stop Loading” button is clicked, it’ll stop the process and will display this:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours