How do I correctly redirect from an existing page to a new URL? It could be an entirely different website/domain
How can I perform a JavaScript redirect to redirect the user from one webpage to another using jQuery or pure JavaScript? - Ask a Question - TestMu AI Community
How can I avoid redirecting when try to download a file using wget or curl?
How To Redirect To A URL In Svelte Kit
If you're trying to redirect server side the best option is going to be to use the loading redirect. https://kit.svelte.dev/docs#loading-output-redirect
Something along the lines of:
<script context="module">
export async function load() {
return {
status: 302,
redirect: "/login"
};
}
</script>I do something similar to make sure users are authenticated.
<script context="module">
export async function load({ session }) {
const { user } = session;
if (! user) {
return {
status: 302,
redirect: "/login"
};
}
return {};
}
</script>If there's no user in the session this would redirect to the login page.
Edit: Code formatting.
More on reddit.comVideos
I'm working with React, although I think the choice of framework has nothing to do with this issue. Just mentioning it for reference.
I have a component which takes in a user's choice/selection from a dropdown. The current choices are sending out an SMS, dialing a number, sending an email and visiting a URL. Once they save their choice, a URL is generated on my domain, which contains a string comprising of the html that is required to perform that action. For example:
tel:phoneNumber (the number they saved goes there)
mailto:emailId?subject=subject&body=body
sms:phoneNumber?body=body websiteURL
I saw quite a few examples online of how to redirect users to a new URL, like this. Let's assume these strings above are stored in a variable called redirectURL
window.location = redirectURL; window.location.href = redirectURL; This is the one I am currently using: window.location.assign = redirectURL
My problem is, the one I am using works for phones, emails and SMS. However if I want to redirect a user from my website, to say google.com, it ends up just adding google.com to the current URL, and not redirecting them to google. How do i fix this?
It never made sense to me that assigning this to window.location would work because location is an object, but it was mentioned on several stackoverflow posts, and I am not very familiar with how the object behaves, in spite of skimming through MDN.
Thanks in advance!