Redirect via JavaScript or jQuery

Some easy and short ways to redirect via pure JavaScript or simple jQuery.


Libraries

Code Snippet
// window.location
window.location.replace('http://www.example.com');
window.location.assign('http://www.example.com');
window.location.href = 'http://www.example.com';
document.location.href = 'http://www.example.com';

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

// jQuery
$(location).attr('href','http://www.example.com')
$(window).attr('location','http://www.example.com')
$(location).prop('href', 'http://www.example.com')


Implementation

All ways above should redirect to www.example.com (except the history-lines). Basically it's not recommend to use jQuery for such an easy thing. See also: Get current URL in JavaScript (https://vsnippets.com/snippet.php?s=57d97f8e8167d&t=Get%20current%20URL%20in%20JavaScript)





Comments
  • mode_editWrite a comment



  • forumComments




Embed Code

We provide special containers which include the full snippet and a link to this site. So for example when you make a tutorial and you are using code from this site, you can use one of the following snippet embeddings:

Load Preview Get Code Close Preview







    Share
    Rating
    thumb_up 1 thumb_down
    Author
    Thorolus

    I'm the head developer and co-founder of GlovilGames Studios (glovilgames.com) and other platforms GlovilGames created, like vsnippets.com, worth-calculator.net, python-obfuscator.com and some mobile apps and games.

    Tags
    Redirect
    Basics
    jQuery



    Informations

    posted on 2016-09-12 19:51:12

    viewed 65 times

    snippet's UUID is 57d6eb10560fb

    Similar Snippets