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

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Redirect to a Relative URL

Using JavaScript to Redirect to a Relative URL

September 7, 2022 Leave a Comment

In JavaScript, we can redirect to a relative URL by using the window.location object and setting it to the relative path we want.

window.location.href = "some-page.com";

Here are some quick relative redirect examples which we will go over below.

window.location.href = "some-page.com";
//This redirect will be relative to our CURRENT directory

window.location.href = "/some-page.com";
//This redirect will be relative to our HOME directory

window.location.href = "../some-page.com";
//This redirect will be relative to one directory ABOVE our current directory

window.location.href = "../../some-page.com";
//This redirect will be relative to TWO directories ABOVE our current directory 

window.location.href = "new-directory/some-page.com";
//This redirect will be relative to the directory new-directory in our current directory

Let’s go over a bunch of examples to show how redirecting to a relative URL in JavaScript works.


In these examples, we will simply show the JavaScript setup and the page you would get redirected to.

window.location.href = "some-page.com";

#Output
https://theprogrammingexpert.com/some-page.com

Since this page is currently in the main directory “https://theprogrammingexpert.com/”, by just putting some-page.com, it will go to this page in the same directory. Hence https://theprogrammingexpert.com/some-page.com.

Let’s show another example assuming we are in a new directory of our site: https://theprogrammingexpert.com/category/javascript/

window.location.href = "some-page.com";

//Assuming our page is located at https://theprogrammingexpert.com/category/javascript/javascript-redirect-to-relative-url

#Output
https://theprogrammingexpert.com/category/javascript/some-page.com

If however, we wanted to go to the relative URL https://theprogrammingexpert.com/some-page.com from our new location, we would simply have to change our JavaScript code a little.

window.location.href = "/some-page.com";

//Assuming our page is located at https://theprogrammingexpert.com/category/javascript/javascript-redirect-to-relative-url

#Output
https://theprogrammingexpert.com/some-page.com

By adding the / before our page, we are saying we want to redirect to our domain directory, and go to the page via that path.

Let’s see another example:

window.location.href = "../some-page.com";

//Assuming our page is located at https://theprogrammingexpert.com/category/javascript/javascript-redirect-to-relative-url

#Output
https://theprogrammingexpert.com/category/some-page.com

The ../ will make us go up one directory from where we were.

And finally:

window.location.href = "another-directory/some-page.com";

//Assuming our page is located at https://theprogrammingexpert.com/category/javascript/javascript-redirect-to-relative-url

#Output
https://theprogrammingexpert.com/category/javascript/another-directory/some-page.com

Hopefully this article has been useful for you to understand how to use JavaScript to redirect to a relative url.

Other Articles You'll Also Like:

  • 1.  How to Convert Radians to Degrees Using JavaScript
  • 2.  How to Convert Degrees to Radians Using JavaScript
  • 3.  JavaScript cos – Find Cosine of Number in Radians Using Math.cos()
  • 4.  JavaScript indexOf – Get the position of a value within a string
  • 5.  Add Minutes to a Date Using JavaScript
  • 6.  Using JavaScript to Replace a Space with an Underscore
  • 7.  Using JavaScript to Remove Substring From String
  • 8.  Using JavaScript to Check if String Contains Letters
  • 9.  Using JavaScript to Wait 5 Seconds Before Executing Code
  • 10.  Remove Parentheses From String Using JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy