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

1
  • please frame your question properly so that you can get maximum out of it Commented Jun 10, 2009 at 11:34

9 Answers 9

65

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.

Sign up to request clarification or add additional context in comments.

5 Comments

i can't use that variable but I can see it in the addressbar. How can I use it?
Hi. Can i use instead of variable a function which returns string?
This doesn't do anything.
This is horrible advice; don't use JavaScript to do what an anchor does naturally; just change the href attribute of the anchor to the URL you want.
While this answer is 11 years old and things would normally be done a bit differently today, I don't think it's fair to say it's horrible advice. For example, if you had 1,000 anchors on the page and each one depended on a variable, would you really suggest to change 1,000 href attributes every time the variable changes? As always, context matters and people's advice are rarely objectively "horrible".
29

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);
})();

Comments

14
<script>
   var scrt_var = 10;
   document.getElementById("link").setAttribute("href",scrt_var);
</script>
<a id="link">this is a link</a>

6 Comments

I think that it would be something like setAttribute("href", "2.html & Key=" + scrt_var) to form the complete link.
Yeah that thing works, Thanks for the short & simple trick :) @erenon
@erenon Is there a way to add target_new to this code to open in a new tab? thank you.
@Andrew: .setAttribute("target", "new") chained after the previous similar call. However, please note pop up windows are rather disliked.
Thanks! That worked. Opening in a new tab is considered a popup window?I agree I dislike advertisement popup windows that show up in a new browser window, but I find it easier to compare two pages or go back to a page when both are in different tabs.
|
14

Alternatively you could just use a document.write:

<script type="text\javascript">
var loc = "http://";
document.write('<a href="' + loc + '">Link text</a>');
</script>

1 Comment

Why is document.write considered a "bad practice"? (it was considered bad practice back in '09 too).
9
<html>

<script language="javascript" type="text/javascript">
var scrt_var = 10; 
openPage = function() {
location.href = "2.html?Key="+scrt_var;
}
</script>

 this is a <a href ="javascript:openPage()">Link  </a>
</html>

Comments

0

You can also use an express framework

app.get("/:id",function(req,res)
{
  var id = req.params.id;
  res.render("home.ejs",{identity : id});
});

Express file, which receives a JS variable identity from node.js

<a href = "/any_route/<%=identity%> includes identity JS variable into your href without a trouble enter

2 Comments

Check your answer code. I am fixed the code but something missing there anyway.
The question shows the variable is in client-side JS. They can't use Express there.
-1

If you use internationalization (i18n), and after switch to another language, something like ?locale=fror ?fr might be added at the end of the url. But when you go to another page on click event, translation switch wont be stable.

For this kind of cases a DOM click event handler function must be produced to handle all the a.href attributes by storing the switch state as a variable and add it to all a tags’ tail.

Comments

-1

JAVASCRIPT CODE:

<script>

    function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

var user = getUrlVars()["user"];
var pass = getUrlVars()["pass"];
var sub  = getUrlVars()["sub"];
</script>

<script>
    var myApp = angular.module('myApp',[]);

myApp.controller('dropdownCtrl', ['$scope','$window','$http', function($scope, $window, $http) {

 $http.get('http://dummy.com/app/chapter.php?user='+user+'&pass='+pass)

     .then(function (response) {$scope.names = response.data.admin;});
    $scope.names = [];

        $http.get('http://dummy.com/app/chapter.php?user='+user+'&pass='+pass+'&sub='+sub)

            .then(function (response) {$scope.chapter = response.data.chp;});
           $scope.chapter = [];

};


}]);
    </script>

HTML:

<div ng-controller="dropdownCtrl">
<div ng-repeat="a in chapter">
<a href="topic.html?ch={{a.chapter}}" onClick="location.href=this.href+'&user='+user+'&pass='+pass+'&sub='+sub;return false;">{{a.chapter}}</a>
</div>

1 Comment

This seems to be an excessively complicated approach … and one which has at least one external dependency that you haven't explained how to load.
-1

Let me try to update with a new answer.

JavaScript Part:

<script language="javascript" type="text/javascript">
    var scrt_var = 10;

    function hrefByVariable(variable = '') {
        window.location.replace('2.html&Key=' + variable);
    }
</script>

HTML Part:

<html>  
    This is a <a href='javascript:hrefByVariable(scrt_var);'>Link</a> 
</html>

These are all pure JavaScript and HTML and will all work, please try.

1 Comment

The hrefByVariable(scrt_var) function is not the same technique as the openPage() function and is more closely related to the main curiosity than the other one. The main curiosity is to "Passing JavaScript variable to <a href>" But I will accept the 'dislike' judgment from everyone as okay.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.