简体   繁体   English

锚href获取当前网址并使用javascript附加在下一页上

[英]anchor href get current url and append on next page with javascript

I would like to get current page url and append it to the next page url on anchor click. 我想获取当前页面的网址,并在锚点单击时将其附加到下一个页面的网址。

html: 的HTML:

<a class="JS-hrefURI" href="">Make reservation</a>

javascript: javascript:

$(function() {
        $(".JS-hrefURI").attr('href', "reservation.php?res=" + document.URL);
 });

When I inspect this anchor on first page load, my href is empty, and after refresh it finally gets current page url appended. 当我在第一页加载时检查此锚点时,我的href为空,并且刷新后最终得到了附加的当前页面URL。 This is kinda working but my page just refreshes on first click and nothing else happens. 这有点工作,但是我的页面在第一次单击时就会刷新,并且什么也没有发生。 On second click on anchor it works well and url is appended. 第二次单击锚点时,效果很好,并附加了网址。

Optionally, instead of whole "document.URL" link, is there a way to pass only specific part of url what comes after "art=" in this example link (/article.php?art=item_id_number) ? 可选地,不是整个“ document.URL”链接,有没有办法仅传递url的特定部分,在此示例链接(/article.php?art=item_id_number)之后?

You can use window.location . 您可以使用window.location

// for your example '/article.php?art=item_id_number'
window.location.search.split('=')[1]; // -> item_id_number

And you should use encodeURIComponent() when constructing the URL. 并且在构造URL时应使用encodeURIComponent()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM