Change URL and redirect using jQuery

前端 未结 6 1486
眼角桃花
眼角桃花 2020-11-27 09:37

I have some code like this,

and now I want to redirect lik

6条回答
  •  隐瞒了意图╮
    2020-11-27 10:28

    As mentioned in the other answers, you don't need jQuery to do this; you can just use the standard properties.

    However, it seems you don't seem to know the difference between window.location.replace(url) and window.location = url.

    1. window.location.replace(url) replaces the current location in the address bar by a new one. The page that was calling the function, won't be included in the browser history. Therefore, on the new location, clicking the back button in your browser would make you go back to the page you were viewing before you visited the document containing the redirecting JavaScript.
    2. window.location = url redirects to the new location. On this new page, the back button in your browser would point to the original page containing the redirecting JavaScript.

    Of course, both have their use cases, but it seems to me like in this case you should stick with the latter.

    P.S.: You probably forgot two slashes after http: on line 2 of your JavaScript:

    url = "http://abc.com/" + temp;
    

提交回复
热议问题