javascript tutorial - [Solved-5 Solutions] How to redirect to another webpage ? - javascript - java script - javascript array



Problem:

How can we redirect the user from one page to another using jQuery or pure JavaScript?

Solution 1:

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 we want to simulate someone clicking on a link, use location.href

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

For example:

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

// similar behavior as clicking on a link
window.location.href = "http:// wikitechy.com";
click below button to copy the code. By JavaScript tutorial team

Solution 2:

This answer has merely been provided as a possible solution; it is obviously not the best solution, as it requires jQuery. Instead, prefer the pure JavaScript solution.

$(location).attr('href', 'http://wikitechy.com')2
click below button to copy the code. By JavaScript tutorial team

Solution 3:


	// window.location
window.location.replace('http://www.wikitechy.com')
window.location.assign('http://www.wikitechy.com')
window.location.href = 'http://www.wikitechy.com'
document.location.href = '/path'

// window.history
window.history.back()
window.history.go(-1)

// window.navigate; ONLY for old versions of Internet Explorer
window.navigate('top.jsp')


// Probably no bueno
self.location = 'http://www. wikitechy.com';
top.location = 'http://www. wikitechy.com';

// jQuery
$(location).attr('href','http://www. wikitechy.com')
$(window).attr('location','http://www. wikitechy.com')
$(location).prop('href', 'http://www. wikitechy.com')
click below button to copy the code. By JavaScript tutorial team

Solution 4:

This works for every browser:

window.location.href = 'your_url';
click below button to copy the code. By JavaScript tutorial team

Solution 5:

we also think that location.replace(URL) is the best way, but if we want to notify the search engines about our redirection (they don't analyze JavaScript code to see the redirection) we should add the rel="canonical" meta tag to your website.

Adding a noscript section with a HTML refresh meta tag in it, is also a good solution. I suggest you to use this JavaScript redirection tool to create redirections. It also has Internet Explorer support to pass the HTTP referrer.

<!-- You are allowed to freely copy, distribute and use this code, but removing author credit is strictly prohibited -->
<!-- Generated by http://insider.zone/tools/client-side-url-redirect-generator/ -->

<!-- REDIRECTING STARTS -->
<link rel="canonical" href="https://wikitechy.com/"/>
<noscript>
    <meta http-equiv="refresh" content="0;URL=https://wikitechy.com/">
</noscript>
<!--[if lt IE 9]><script type="text/javascript">var IE_fix=true;</script><![endif]-->
<script type="text/javascript">
    var url = "https://wikitechy.com/";
    if(typeof IE_fix != "undefined") // IE8 and lower fix to pass the http referer
    {
        document.write("redirecting..."); // Don't remove this line or appendChild() will fail because it is called before document.onload to make the redirect as fast as possible. Nobody will see this text, it is only a tech fix.
        var referLink = document.createElement("a");
        referLink.href = url;
        document.body.appendChild(referLink);
        referLink.click();
    }
    else { window.location.replace(url); } // All other browsers
</script>
<!-- Credit goes to http://insider.zone/ -->
<!-- REDIRECTING ENDS -->
click below button to copy the code. By JavaScript tutorial team

Related Searches to javascript tutorial - How to redirect to another webpage ?