How to get through the "question mark" in javascript url

In an Angularjs app, I have a url like
http://url.com/my_app/#/store/items .
Now I want to add a query string, for example,
http://url.com/my_app/#/store/items?page=2 .

but in url javascript encodes "?" to "%3F" "?" to "%3F" , which I do not want. It should remain "?" Only in the URL, since angularjs $ location.search () does not return anything for "% 3F".

How can I do that?

+38
javascript angularjs
Mar 04 '13 at 9:01
source share
5 answers

There is not enough detail in your question, so I assume that you are using AngularJS routing - or at least $ location service - in a mode other than HTML5. If so, the part after the # character represents your URL in terms of a one-page application (more about AngularJS here ).

If the above assumptions are true, this means that you should not try to add or manipulate the question mark “manually” . Instead, you should change the search $location part to manipulate the query string (part after ? ), And the question mark will be added / removed to the final URL as needed.

In your case, you can write:

 $location.path('/store/items').search('page', 2) 

This assumes that you are manipulating URLs from JavaScript, as stated in your question.

+80
Mar 04 '13 at 16:23
source share

If you use the $ location service, use $location.url('/store/items?page=2') instead. It has been a setter method since at least 1.0.7 and works in my 1.1.5 application.

+24
Jan 09 '14 at 12:52
source share

you can create a parameter object, for example:

 var param = { page: 2 } $location.url("/store/items").search(param) 
+2
May 05 '15 at 12:11
source share

If you are using ui-router , which is highly recommended, you can use $state.go(to, params, options) as described here .

As a prerequisite, you need to correctly determine your state, which means that every possible request parameter must be known by ui-router. See the following example ( page and otherParam ):

 $stateProvider. state('storeItems', { url: '/store/items?page&otherParam', templateUrl: '/modules/store/views/item.client.view.html' }); 

And then you can simply switch places for the instance from the controller by calling

 $scope.gotoItemsPage = function(page) { $state.go('storeItems', { page: page, otherParam: 'Just a show off' }); }; 

No coding required and readable!

+1
Jan 04 '15 at 17:30
source share

You can use decodeURIComponent .

For example:

 decodeURIComponent('http://url.com/my_app/#/store/items%3Fpage=2'); // will give you `http://url.com/my_app/#/store/items?page=2` 
-2
Mar 04 '13 at 9:06 on
source share



All Articles