<script language="javascript" type="text/javascript"> var scrt_var = 10; </script>
HTML Part:
<html> this is a <a href ="2.html & Key= scrt_var">Link </a> </html>
I just want to sent the javascript variable to link (url parameter)
No AJAX
href”, append the variable to it (Here we have used a variable named “XYZ”). Then we need to append the value to the URL. Now our URL is ready with the variable and its value appended to it. In the example below, we will append a variable named 'XYZ' and its value is 55.
To add a URL variable to each link, go to the Advanced tab of the link editor. In the URL Variables field, you will enter a variable and value pair like so: variable=value. For example, let's say we are creating links for each store and manager.
You cannot use js variables inside html. To add the content of the javascript variable to the html use innerHTML() or create any html tag, add the content of that variable to that created tag and append that tag to the body or any other existing tags in the html.
In JavaScript, you can call a function or snippet of JavaScript code through the HREF tag of a link. This can be useful because it means that the given JavaScript code is going to automatically run for someone clicking on the link. HREF refers to the “HREF” attribute within an A LINK tag (hyperlink in HTML).
If you want it to be dynamic, so that the value of the variable at the time of the click is used, do the following:
<script language="javascript" type="text/javascript"> var scrt_var = 10; </script> <a href="2.html" onclick="location.href=this.href+'?key='+scrt_var;return false;">Link</a>
Of course, that's the quick and dirty solution. You should really have a script that after DOM load adds an onclick handler to all relevant <a>
elements.
put id attribute on anchor element
<a id="link2">
set href attribute on page load event:
(function() { var scrt_var = 10; var strLink = "2.html&Key=" + scrt_var; document.getElementById("link2").setAttribute("href",strLink); })();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With