admin管理员组文章数量:1136158
<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
<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
Share Improve this question edited May 5, 2014 at 23:59 Qantas 94 Heavy 16k31 gold badges72 silver badges88 bronze badges asked Jun 10, 2009 at 11:32 joejoe 35.1k30 gold badges103 silver badges140 bronze badges 1- please frame your question properly so that you can get maximum out of it – Vinay Pandey Commented Jun 10, 2009 at 11:34
8 Answers
Reset to default 64If 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);
})();
Alternatively you could just use a document.write:
<script type="text\javascript">
var loc = "http://";
document.write('<a href="' + loc + '">Link text</a>');
</script>
<script>
var scrt_var = 10;
document.getElementById("link").setAttribute("href",scrt_var);
</script>
<a id="link">this is a link</a>
<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>
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
If you use internationalization (i18n), and after switch to another language, something like ?locale=fr
or ?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.
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>
本文标签: Passing Javascript variable to lta href gtStack Overflow
版权声明:本文标题:Passing Javascript variable to <a href > - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736958231a1957664.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论