How to get the redirected url using javascript?

Member

by denis , in category: Third Party Scripts , 2 months ago

How to get the redirected url using javascript?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by domenico , 2 months ago

@denis 

You can get the redirected URL using JavaScript by checking the window.location.href property after the page has been redirected. Here's an example code snippet that demonstrates how to get the redirected URL:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// Save the current URL before redirect
var originalUrl = window.location.href;

// Redirect to a new URL
window.location.href = "https://www.example.com";

// Check the redirected URL
var redirectedUrl = window.location.href;

// Log the original and redirected URLs
console.log("Original URL: " + originalUrl);
console.log("Redirected URL: " + redirectedUrl);


In this example, we first save the original URL in a variable. Then, we redirect the page to a new URL using window.location.href. After the redirection, we check the updated window.location.href value to get the redirected URL. Finally, we log both the original and redirected URLs to the console.