Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass "question mark" in url javascript

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

but in url, javascript encodes the "?" to "%3F" which i don't want. It should remain "?" only in the url as angularjs $location.search() returns nothing for "%3F".

How it can be done ?

like image 249
Amb Avatar asked Mar 04 '13 09:03

Amb


People also ask

Can I use question mark in URL?

Yes, it is valid. Only the first ? in a URL has significance, any after it are treated as literal question marks: The query component is indicated by the first question mark ("?") character and terminated by a number sign ("#") character or by the end of the URI.

Is there a way to pass JavaScript variables in URL?

To put a variable in a string enclose the variable in quotes and addition signs like this: var myname = "BOB"; var mystring = "Hi there "+myname+"! "; Just remember that one rule!

What does a question mark mean in a URL?

The question mark ("?", ASCII 3F hex) is used to delimit the boundary between the URI of a queryable object, and a set of words used to express a query on that object. When this form is used, the combined URI stands for the object which results from the query being applied to the original object.


2 Answers

There is not enough details in your question so I will assume that you are using AngularJS routing - or at least the $location service - in non-HTML5 mode. If so, the part after the # character represents your URL from the single-page-application point of view (more about AngularJS here).

If the above assumptions are correct it means that you shouldn't try to add or manipulate the question mark "by hand". Instead you should change the search part of the $location to manipulate query string (part after ?) and the question mark will be added / removed to the final URL as needed.

In your case you could write:

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

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

like image 108
pkozlowski.opensource Avatar answered Sep 23 '22 13:09

pkozlowski.opensource


If you are using the $location service then use $location.url('/store/items?page=2') instead. This has been a setter method from at least 1.0.7 and works a treat in my 1.1.5 app.

like image 42
toxaq Avatar answered Sep 19 '22 13:09

toxaq