Introduction
In web development, there are scenarios where you need to redirect users from their current page to another within your website. This can be a simple redirection or involve navigating through directories on the site. When using JavaScript for this task, understanding how relative URLs work becomes crucial. This tutorial explores how to perform URL redirection in JavaScript, focusing specifically on moving up one directory level or redirecting to a different path relative to the domain.
What is URL Redirection?
URL redirection is the process of automatically forwarding users from one web page address (URL) to another. In client-side programming using JavaScript, this is typically done with properties and methods available in the window.location
object. The primary properties used for redirection are location.href
, which can be set to a new URL, thereby redirecting the user.
Types of URLs
Before diving into relative URL redirection, let’s understand different types of URLs you might encounter:
- Absolute URL: Contains the full path including scheme (e.g.,
https
), domain, and resource path. Example:https://domain.com/path
. - Relative to Current Schema: Starts with two slashes (
//
) followed by the domain and path. It inherits the current protocol from the browser. Example://domain.com/path
. - Relative to Current Path: Starts directly with a path without specifying the domain, inheriting both scheme and domain from the current URL. Example:
/path
orfolder/index.php?file=abc&test=123&lol=cool
. - One Level Up: Uses
../
, indicating that the browser should move up one directory in the hierarchy.
Redirecting to a Relative URL
Moving One Directory Level Up
When you need to redirect to a parent directory, JavaScript provides a straightforward syntax:
window.location.href = '../';
This code snippet redirects the user to the immediate parent directory of the current page. The ../
notation is universally recognized in file systems and web URLs as an instruction to move up one level.
Redirecting Relative to Domain
To redirect users to a specific path relative to the root domain, use:
window.location.href = '/path';
This method is useful when you want to navigate to a known directory within your site from any page, ensuring that the full path starts at the domain’s base.
Using Location
Methods
JavaScript’s location
object also provides methods like assign()
, which can achieve similar results:
window.location.assign("../"); // One level up
The assign()
method is equivalent to setting location.href
, and it loads a new document. Unlike the replace()
method, using assign()
does not remove the current page from the session history, meaning users can press the back button to return.
Practical Example
Consider you are on the page located at:
example.com/path/folder/index.php?file=abc&test=123&lol=cool
If your goal is to redirect to example.com/path/
, you could use any of the following methods depending on whether you want to keep query parameters or not:
-
To move up one directory and remove any existing query string:
window.location.href = '../';
-
To directly specify the path relative to the domain, disregarding the current file’s query parameters:
window.location.href = '/path/';
Conclusion
Understanding how to manipulate URLs for redirection in JavaScript is a fundamental skill in web development. It allows developers to navigate users through their site efficiently and create a seamless user experience. By mastering relative URL redirection, you can handle various scenarios from moving up directories to redirecting based on the domain root with ease.
Remember that choosing between location.href
, location.assign()
, or other properties should depend on your specific use case, especially considering how these methods interact with browser history and session management.