使用jQuery更改URL和重定向 [英] Change URL and redirect using jQuery

查看:155
本文介绍了使用jQuery更改URL和重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些像这样的代码,

I have some code like this,

<form id="abc">
  <input type="text" id="txt" />
</form>

现在我想像这样重定向,

and now I want to redirect like this,

var temp = $("#txt").val();
url = "http:abc.com/" + temp;
window.location.replace(url);
or
window.location(url);

jQuery中有没有解决这个问题?它仍然让我有 url = http://abc.com

Is there anyway in jQuery to solve this? It still lets me have url = http://abc.com.

推荐答案

正如其他答案所述,你不需要jQuery来做这件事;你可以使用标准属性。

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

然而,你似乎并不知道 window.location.replace(url)之间的区别) window.location = url

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)用新的替换地址栏中的当前位置。调用该函数的页面不会包含在浏览器历史记录中。因此,在新位置,单击浏览器中的后退按钮将使您返回到您在查看包含重定向JavaScript的文档之前查看的页面。

  2. window.location = url 重定向到新位置。在这个新页面上,浏览器中的后退按钮将指向包含重定向JavaScript的原始页面。

  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.

PS:你可能在 http之后忘了两个斜杠:在JavaScript的第2行:

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

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

这篇关于使用jQuery更改URL和重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆