javascript - Node.js - Get ID of href in app.js - Stack Overflow

I am using an href to link the user to a route. My problem is that I need to send a parameter to the da

I am using an href to link the user to a route. My problem is that I need to send a parameter to the dashboard while clicking on the link. The easiest way to do this would be to get the id or name of the href in the app.js and send it to the dashboard.

Here is my code:

 //the href in the index.ejs
 <a href="/dashboard" id="ANDROID">ANDROID</a> 

 //in the app.js
 app.get('/dashboard', function(req, res) {
     var url = req.url;
     console.log("url: " + url);    //prints out url: /dashboard
     var id = req.url.id;
     console.log("id: " + id);      //prints out id: undefined
 });

How can I grap the id or name of the href?

I am using an href to link the user to a route. My problem is that I need to send a parameter to the dashboard while clicking on the link. The easiest way to do this would be to get the id or name of the href in the app.js and send it to the dashboard.

Here is my code:

 //the href in the index.ejs
 <a href="/dashboard" id="ANDROID">ANDROID</a> 

 //in the app.js
 app.get('/dashboard', function(req, res) {
     var url = req.url;
     console.log("url: " + url);    //prints out url: /dashboard
     var id = req.url.id;
     console.log("id: " + id);      //prints out id: undefined
 });

How can I grap the id or name of the href?

Share Improve this question asked Oct 9, 2014 at 12:09 sanyoohsanyooh 1,6412 gold badges21 silver badges35 bronze badges 1
  • It's hard to know what you are actually trying to achieve, but at a guess I'd say that it's because req.url only returns the url and doesn't have information on the link that it came from. I'd consider argument passing, express facilitates this: expressjs./4x/api.html#app.param – NMunro Commented Oct 9, 2014 at 12:20
Add a ment  | 

1 Answer 1

Reset to default 3

If I understand correctly, you need to pass the parameter to the URL. The ID of the "a" element is only a DOM ID and it is not in any way connected to the URL the link leads to.

You could try something like this:

<a href="/dashboard?id=ANDROID" id="ANDROID">ANDROID</a>

Then in the code:

app.get('/dashboard', function(req, res) {
    var id = req.query.id;
    ...
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745335868a4623100.html

相关推荐

  • javascript - Node.js - Get ID of href in app.js - Stack Overflow

    I am using an href to link the user to a route. My problem is that I need to send a parameter to the da

    7小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信