简体   繁体   English

在可点击链接中传递 JavaScript 变量

[英]passing JavaScript variable in clickable link

I want to add part of the current url to the next page(link click).我想将当前 url 的一部分添加到下一页(点击链接)。 So basically, I take the url after '?'所以基本上,我在 '?' 之后获取 url。 and attach it to the end of a link.并将其附加到链接的末尾。 How can I use finalurl as the link?如何使用 finalurl 作为链接?

<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您可以通过调用JavaScript方法使用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.将 ID 添加到您的a并使用jQueryJavaScript放入其中。

 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使用 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 ?.基于 URL 您需要将 / 更改为 ?。 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我已经使用 jquery 做到了这一点,我希望你会发现它有帮助

 <!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>

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

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