将当前 URL 中的 location.hash 附加到目标 URL

Append location.hash from current URL to target URL

我有一个 url,末尾有一个哈希值:test123.com/go#whathaveyou

我如何将散列值附加到点击按钮 url 中的目标 url 目标:

<form target="_blank" id="form1">
<input onclick="window.location.href='test123.com/newpage'+location.hash;" type="Submit" value="GO" style="border-radius: 5px;"/></form>

最终目标是转到附加哈希值的新页面,因此:test123.com/newpage#whathaveyou

假设您为该输入提供 id "link":

var elem = document.getElementById('link');
var url = window.location.href;
var res = url.split("#");
var hash = res[1];
elem.addEventListener('click, function () {
    window.location = https://test123.com/newpage'+ hash;
});

您可以将此代码压缩很多,只是希望它尽可能具有可读性。

你也可以使用这个:

<input onclick="window.location=(window.location.href).split('#')[1]" type="Submit" value="GO" style="border-radius: 5px;"/></form>

简短明了