How to Redirect with JavaScript

How do I redirect to another webpage?

One does not simply redirect using jQuery

jQuery is not necessary, and window.location.replace(...) will best simulate an HTTP redirect.

window.location.replace(...) is better than using window.location.href, because replace() does not keep the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco.

If you want to simulate someone clicking on a link, use
location.href

If you want to simulate an HTTP redirect, use location.replace

For example:

// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");

// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";

How do I redirect with JavaScript?

To redirect to another page, you can use:

window.location = "http://www.yoururl.com";

How do I redirect to another webpage?

One does not simply redirect using jQuery

jQuery is not necessary, and window.location.replace(...) will best simulate an HTTP redirect.

window.location.replace(...) is better than using window.location.href, because replace() does not keep the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco.

If you want to simulate someone clicking on a link, use
location.href

If you want to simulate an HTTP redirect, use location.replace

For example:

// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");

// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";

JS - Redirect and return to home page

I resolved this way:

<script>
let urls = JSON.parse(localStorage.getItem('urls') || '[-1]')
let urlList = ['page2.com', 'page3.com', 'page4.com']

if(urls[0] == -1) { // fresh start
localStorage.setItem('urls', JSON.stringify(urlList))
startRedirection('page1.com')
} else if(urls.length == 0) { // all redirects done
console.log("Exhausted")
localStorage.removeItem('urls') // removes the key "urls" for a fresh start
location.reload();
} else {
const url = urls.shift() // get the first url in "urls" array and remove it
from the array
localStorage.setItem('urls', JSON.stringify(urls)) // update the state
startRedirection(url) // redirect
}

function startRedirection(url) {
setTimeout(() => location.href = url, 2000)
}
</script>

How to redirect users when they access the website's view page source?

The example you linked in the comments of your original question uses a header-based redirect, which is why when you point your instance of Chrome to view-source:rshrt.com/getlink/YCy, it redirects you to the target page. This is in line with how browsers are expected to handle header-based redirects. This fact is confirmed if you open your developer tools' Network tab and inspect the requests that your browser sends to the target site, which show the rshrt.com page redirecting with a 301 Moved Permanently status code (along with the respective Location header, which points to the target page):

Sample Image


In your own example, you're performing a redirect using JavaScript. This JavaScript isn't executed/interpreted by the browser when using view-source. As such, the redirection never happens.

If you'd like this to function moreso like the site you've given as an example, you'll have to configure these redirection rules at a server level to correctly return a 301 or 302 status code (as well as the appropriate Location header), instead of on the client level using JavaScript. How specifically you would accomplish that is entirely dependent on the stack your server runs, and is arguably outside the current scope of the question as you've posed it.

How to redirect in JavaScript to another website after establishing an authenticated session using PHP (Codeigniter)

As it turns out, the proposed solution in the question actually is preferred. Since the token of the user is so personalized and it is even stored in the server session and stored in the database, it would so difficult to authenticate you're self using someone else's authentication data. Thus, there is no major security vulnerability. The idea of exposed data scared me that it blinded me from seeing that exposing the token isn't a huge security vulnerability.

How to redirect page after POST by fetch

When you click the submit button, it triggers the JS but while fetch is asynchronously making an HTTP request the form submits normally. This triggers navigation and cancels the JS.

You need to prevent the default behaviour of the click on the submit button.

register = async (clickEvent) => {
clickEvent.preventDefault();

NB: Generally should should be binding to the submit event on the form and not the click event of the submit button.



Related Topics



Leave a reply



Submit