简体   繁体   English

将 Javascript 变量传递给

[英]Passing Javascript variable to <a href >

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

HTML Part: HTML部分:

<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)我只想将 javascript 变量发送到链接(url 参数)

No AJAX没有 AJAX

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.你真的应该有一个脚本,在 DOM 加载后,为所有相关的<a>元素添加一个 onclick 处理程序。

put id attribute on anchor element将 id 属性放在锚元素上

<a id="link2">

set href attribute on page load event:在页面加载事件上设置 href 属性:

(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:或者,您可以只使用 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 Express 文件,它从 node.js 接收一个 JS 变量标识

<a href = "/any_route/<%=identity%> includes identity JS variable into your href without a trouble enter <a href = "/any_route/<%=identity%>身份 JS 变量包含到您的href没有问题输入

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.如果您使用国际化 (i18n),并在切换到另一种语言后,可能会在 url 末尾添加诸如?locale=fr?fr之类的内容。 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.对于这种病例DOM click事件处理函数必须生产来处理所有的a.href通过存储开关状态为变量的属性,并将其添加到所有a标签的尾部。

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

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

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