I want to add part of the current url to the next page(link click). So basically, I take the url after '?' and attach it to the end of a link. How can I use finalurl as the link?
<html>
<body>
<script>
var codes = window.location.href.split('?')[1];
var finalurl = "www.google.com/" + codes;
</script>
<p><a href="finalurl">ChangedNextUrl</a></p>
</body>
</html>
You can use it by invoking a javascript method from a
<html> <body> <script> function goToFinalURL() { var codes = window.location.href.split('?')[1]; console.log(codes); var finalurl = "https://www.google.com/" + codes; window.location.href = finalurl; } </script> <p><a href="javascript:goToFinalURL()">ChangedNextUrl</a></p> </body> </html>
Add ID into your a
and use jQuery or JavaScript for put inside it.
var codes = window.location.href.split('?')[1]; var finalurl = "www.google.com/" + codes; document.getElementById("finalUrl").href = finalurl;
<p><a id="finalUrl" href="finalurl">ChangedNextUrl</a></p>
You can edit the code to become like this:
var codes = window.location.href.split('?')[1]; var finalurl = "https://www.google.com/" + codes; a = document.querySelector('a'); a.setAttribute("href",finalurl );
<p><a href="">ChangedNextUrl</a></p>
using JQuery
$("#id").click(function(){ var myVar = "addme"; var href = window.location.href+myVar; window.location.href = href; });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script> <p><a href="finalurl" id="id">ChangedNextUrl</a></p>
Here you go with one more solution
$('a').click(function(){ var codes = window.location.href.split('?')[1]; var finalurl = "www.google.com/" + codes; window.location.href = finalurl; });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p><a href="">ChangedNextUrl</a></p>
Hope this will help you.
Based ON URL You need to change / to ?. it will work. or else provide your url.
<html> <body> <script> function final_url() { var codes = window.location.href.split('/')[2]; var finalurl = "www.google.com/" + codes; window.location.href = finalurl; } </script> <p><a href"#" onclick="final_url()">ChangedNextUrl</a></p> </body> </html>
I have done this by using jquery i hope u will find it helpfull
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script> <script type="text/javascript"> $(document).ready(function () { var codes = window.location.href; codes= codes.substr(codes.lastIndexOf('/')+1); var finalurl = "https://stackoverflow.com/posts/46769492/" + codes; $('#links').attr('href', finalurl); }); </script> <title></title> </head> <body> <a id="links" href="https://www.google.co.in/">ChangedNextUrl</a> </body> </html>
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.