简体   繁体   中英

Append location.hash from current URL to target URL

I have a url with a hash value at the end: test123.com/go#whathaveyou

How would I go about appending the hash value to the target url in a button onclick url target:

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

End goal is to go to the new page with the hash value appended so: test123.com/newpage#whathaveyou

Assuming you give that input the 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;
});

You can condense this code a lot, just wanted it to be as readable as possible.

You can use this also:

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

Short and clear

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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