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

javascript - How to get url parameters in Backbone? - Stack Overflow

programmeradmin6浏览0评论

I'm using Backbone with Marionette.

I have a link <a> tag where I'm passing few parameters, how can I extract those values in other pages using Backbone?

<a href=":8080/help/?name=matth&age=25&[email protected]">View Details</a>

Address Bar url:

:8080/help/?name=matth&age=25&[email protected] 44

Using Php, this is straightforward:

$Url = $_GET['state']."#".$_GET['city']; 

How can I achieve it within my Backbone app?

I'm using Backbone with Marionette.

I have a link <a> tag where I'm passing few parameters, how can I extract those values in other pages using Backbone?

<a href="http://localhost.:8080/help/?name=matth&age=25&[email protected]">View Details</a>

Address Bar url:

http://localhost.:8080/help/?name=matth&age=25&[email protected] 44

Using Php, this is straightforward:

$Url = $_GET['state']."#".$_GET['city']; 

How can I achieve it within my Backbone app?

Share Improve this question edited Feb 13, 2017 at 15:57 Emile Bergeron 17.4k5 gold badges85 silver badges131 bronze badges asked Feb 13, 2017 at 11:20 KhalidKhalid 4673 silver badges20 bronze badges 1
  • Possible duplicate of navigate route with querystring – Emile Bergeron Commented Feb 13, 2017 at 15:59
Add a ment  | 

1 Answer 1

Reset to default 5

If the route is defined with something like this:

'help/:name&:age&:email' : 'help'

Then you can access those params in the help function just by defining them in the signature of the method (within the backbone router),

help: function(name, age, email) {
    // do whatever you want with the params
}

In your case, this will give you params like this:

name="XXX" age="XXX"

So the proper routing would be

'help/?(name=:name)(&age=:age)(&email=:email)' : 'help'

Where parentheses make a part optional.

Backbone docs

Routes can contain parameter parts, :param


Note that the order is important and the following url wouldn't trigger the route callback. Notice the email and age params placement.

help/?name=test&email=test%40example.&age=6

In order to trigger a route regardless of the number of params and their ordering, take a look at how to parse the query string in the route function, but that won't always work.

发布评论

评论列表(0)

  1. 暂无评论