• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Basics / How To Automatically Redirect To Another URL (JavaScript and jQuery)

How To Automatically Redirect To Another URL (JavaScript and jQuery)

How To Automatically Redirect To Another URL (JavaScript and jQuery)

JavaScript and jQuery are both popular JavaScript libraries that can be used to redirect a page to another URL on page load. In this article, we will explain how to accomplish this task using both JavaScript and jQuery, with code examples for each method.

Using JavaScript:

The simplest way to redirect a page to another URL on page load using JavaScript is to use the location.href property. This property can be set to any valid URL, and the browser will automatically redirect the page to that URL. Here is an example of how to use this method:

window.onload = function() {
  location.href = "https://www.example.com";
}

In this example, the onload event is used to trigger the redirect function, which sets the location.href property to the desired URL. This code should be placed in the head of your HTML document, or in an external JavaScript file that is included in your HTML document.

Using jQuery:

Another way to redirect a page to another URL on page load using jQuery is to use the ready function, which is triggered when the page has finished loading. Here is an example of how to use this method:

$(document).ready(function() {
  window.location.href = "https://www.example.com";
});

In this example, the ready function is used to trigger the redirect function, which sets the location.href property to the desired URL. This code should be placed in the head of your HTML document, or in an external JavaScript file that is included in your HTML document.

Note: you need to include jQuery library before using it.

Basics, Miscellaneous

Related Snippets:

  • The JavaScript function Math.pow()
  • Combine all items in an array into a string
  • Check if the provided value is of the specified type
  • Convert a string into an array

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2023 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us