Redirect an address with javascript

Asked

Viewed 1,314 times

0

I have a url x and every time the user type this url in browser, I want him to be automatically directed to url y, how to do this with javascript or jQuery?

  • Have you tried window.Location = "http://www.algumaurl.com.br";

  • 2

    For specific url I think it best to redirect in htaccess... Otherwise if you really want to do it in js you will have to take the current url, compare if it is url "X" if yes you do a window.location.href, but for this type of thing I think htaccess better

  • What is the advantage of this? Puts the contents of the page with url y in the page of url x

  • Have you found a solution to this question?

2 answers

2

jQuery

$(document).ready(function(){ window.location.href ="url y"; });

Javascript

<body onload="window.location.href = 'url y';">

1

Look here, buddy. Although it is possible, this redirect method is not very recommended because it only runs after all the code is loaded. There are other more efficient methods...

With PHP:

header("location: https://seusite.com.br");

With the HTML metatag Refresh

<meta http-equiv="refresh" content="0; URL='https://seusite.com.br'"/>

Content is equivalent to waiting time to redirect.

Javascript-enabled:

window.location.href='https://seusite.com.br';

With jQuery:

$(location).attr('href', 'http://seusite.com.br');

Although it is not necessary to use jQuery to redirect pages.

Then it’s up to you which one to use.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.