使用jQuery更改URL和重定向

编程入门 行业动态 更新时间:2024-10-26 22:21:01
本文介绍了使用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/" + temp; window.location.replace(url); or window.location(url);

jQuery中有没有解决这个问题?它仍然让我有 url = abc 。

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

推荐答案

正如其他答案所述,你不需要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.

  • window.location.replace(url)用新的替换地址栏中的当前位置。调用该函数的页面不会包含在浏览器历史记录中。因此,在新位置,单击浏览器中的后退按钮将使您返回到您在查看包含重定向JavaScript的文档之前查看的页面。
  • window.location = url 重定向到新位置。在这个新页面上,浏览器中的后退按钮将指向包含重定向JavaScript的原始页面。
  • 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.
  • 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 = "abc/" + temp;

    更多推荐

    使用jQuery更改URL和重定向

    本文发布于:2023-10-10 12:06:25,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1478627.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:重定向   jQuery   URL

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!