Ask coding questions

← Back to all posts
How to redirect to a URL in java-script?
ARJPEG (154)

I am making a website, and want to have an confirm that (if true) redirects to a different page. PLS help

Answered by Kognise (459) [earned 5 cycles]
View Answer
Comments
hotnewtop
Kognise (459)

Google it, the first result should help. For the confirmation dialog, Google that too.

ARJPEG (154)

@Kognise Aldredy did,

It says 'window.location.redirect(url)

Kognise (459)

@adityaru so do that. window.location.redirect("https://example.com/")

ARJPEG (154)

@Kognise No, It just does something like: currentWebsite.com/urlToRedirectTo

Kognise (459)

@adityaru What URL are you trying to redirect to? Make sure it's valid and not a relative path.

Kognise (459)

@adityaru Is that the URL you're redirecting to or the location of the code in question? window.location.href = "http://uac.amitkoduru.repl.co/" works fine for me

Kognise (459)

@adityaru (see my edit, I think window.location.redirect doesn't actually exist)

Kognise (459)

@adityaru Consider whether or not you're replacing the current page or opening a new page - with window.location.href = "http://uac.amitkoduru.repl.co/" the browser back button will still work