【问题标题】:Create a link to another directory based on the current URL根据当前 URL 创建指向另一个目录的链接
【发布时间】:2020-08-28 17:14:35
【问题描述】:

我的访问者在一个具有如下 URL 的页面上:

https://example.com/dir1/foo/page.php?a=search&cid=&topicId=11&q=

我想在页面上显示一个链接,单击该链接会将它们重定向到另一个目录,但有关该 URL 的所有其他内容都将保持不变:

https://example.com/dir1/dir2/dir3/foo/page.php?a=search&cid=&topicId=11&q=

在我看来,一个人可以使用这样的东西......

window.location.href;

...然后将dir1/ 替换为dir1/dir2/dir3/。但这超出了我的想象。

【问题讨论】:

  • location.href = location.href.replace("dir1", "dir1/dir2/dir3")

标签: javascript jquery url


【解决方案1】:

谢谢@freedomn-mn

我最终得到了这个可行的解决方案:

<a id="new-url" onclick="location.href = location.href.replace('/dir1', '/dir1/dir2/dir3')">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    • 2012-03-24
    • 2015-06-05
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多