How to get the redirected url using javascript?

Member

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

How to get the redirected url using javascript?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by domenico , 5 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.

Related Threads:

How to get the final redirected url using php?
How to get the redirected url in golang?
How to get a variable from a url by javascript?
How to get # url value with javascript?
How to get url parameters using shopify liquid?
How to fetch data from a url using javascript?