0

In my project I have got a "back button" that is calling a javaScript function that remembers the previous url when called.

//back button function 
function goBack() { $window.history.back(); }

the url being remembered or being called when the back button is clicked contains an ID and this is ID is used to filter data. The main problem i'm having is that this ID is being returned as a string encoded in html. for example this is the url being called:

//Lookups?$filter=VehicleId%20eq%20%272415%27&

As you can see VehicleId is 2415 in above url, which is also encoded to %20%272415%27&. this in plain text is VehicleId = "2415". This is clashing with my program as my program is expecting an int variable instead of a string.

To resolve this i am trying to de -encode?? from html to make the ID to an int. and i having been looking at this http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_unescape but i still can't figure how to put this in my project.

I am using $location in order to remember the ID in url like so:

if ($location.search().vehicle) {
                        var str = vehicle;
                        var strEsc = unescape(str);
                        vehicle = strEsc;

                      vehicle = $location.search().vehicle;

                    }

To be honest i don't if i am doing this right (which i'm not sure) ..Is any one to help with this please?

  • possible duplicate of [Javascript equivalent to php's urldecode()](http://stackoverflow.com/questions/3431512/javascript-equivalent-to-phps-urldecode) – Andrzej Doyle Nov 14 '14 at 09:50
  • 1
    I would also be very wary of providing a Javascript-backed, form-based Back button, and telling your users not to use the browser's Back button. They won't listen/notice. Make sure the browser's back button does what the user expects - at which point, you don't need your HTML one. – Andrzej Doyle Nov 14 '14 at 09:52
  • Thank you for taking your to respond to my question and I see where you're coming from but in this project there only user .. who as request for this exact function. – user3880405 Nov 14 '14 at 09:59
  • can you share the code that "that remembers the previous url when called." ? – Florent Nov 14 '14 at 10:01
  • its up there sir!/// >> function goBack() { $window.history.back(); } – user3880405 Nov 14 '14 at 10:06
  • your url parameter is indeed `VehicleId eq '2415'` and I bet it's not HTML – Florent Nov 14 '14 at 10:19

2 Answers2

1

decodeURIComponent is the key:

decodeURIComponent('VehicleId%20eq%20%272415%27&'); // "VehicleId eq '2415'&"

That's definitely not a proper ID (not to mention you're trying to do $filter=VehicleId=2135, of course that will break the query string, find a way to encode that value). You should probably make those IDs a bit more standard as then you'd get more accurate results.

Shomz
  • 37,421
  • 4
  • 57
  • 85
  • hmmm.... do you know how i can set url to change accordingly? i mean the vehicle ID will change dynamically according the users selections. – user3880405 Nov 14 '14 at 10:33
  • I'd suggest using a more proper URL structure, for example: `/Lookups?VehicleId=2415` because your current structure will always have to me rewritten manually (changing ` eq ` to `=`, for example) and it's probably not worth it. – Shomz Nov 14 '14 at 11:59
0

You could use regexp to parse the VehicleId, such as :

var tab=unescape(location.search).match(/VehicleId eq '(\d+)'/);
if (tab) {
    var id=tab[1];
}
Florent
  • 391
  • 1
  • 12