最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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;
    ...
});
发布评论

评论列表(0)

  1. 暂无评论