• 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 / Miscellaneous / Redirect to a specified URL

Redirect to a specified URL

Redirect to a specified URL

Use Window.location.href or Window.location.replace() to redirect to url. Pass a second argument to simulate a link click (true – default) or an HTTP redirect (false).

const redirect = (url, asLink = true) =>
  asLink ? (window.location.href = url) : window.location.replace(url);

// EXAMPLE
redirect('https://google.com');

Source

https://www.30secondsofcode.org/js/s/redirect

Miscellaneous

Related Snippets:

  • Sanitize an HTML string to reduce the risk of XSS attacks
  • Remove leading and trailing whitespace from a string
  • Detect If an Element has focus
  • Transform the items in an array and create a new one

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