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?

Dani AI

Generated

Short answer: the string in the URL has two different encodings to undo — percent-encoding (like %20, %27) and HTML entities (like &). Use the standard URL decoder first (not the deprecated unescape()), then decode HTML entities; finally validate/convert the result to a number. (developer.mozilla.org)

A robust, minimal routine (browser) — decode percent-escapes, fall back if malformed, then convert HTML entities with the DOM parser:

function decodeParamWithEntities(s) {
  try {
    // decodeURIComponent will throw on malformed % sequences
    s = decodeURIComponent(String(s).replace(/\+/g, ' '));
  } catch (err) {
    // try a looser decode if needed
    try { s = decodeURI(String(s)); } catch (e) { /* final fallback: leave as-is */ }
  }
  // decode HTML entities (turns "&" into "&")
  const doc = new DOMParser().parseFromString(s, 'text/html');
  return doc.documentElement.textContent;
}

Use that function on the value you read from Angular or the URL and then convert with a strict check:

const raw = $location.search().vehicle || document.referrer || '';
const decoded = decodeParamWithEntities(raw);
if (/^\d+$/.test(decoded)) {
  const vehicleId = parseInt(decoded, 10);
  // safe to use as integer
} else {
  // handle invalid value
}

$location.search() often gives you parsed query params (so check it first to avoid double-decoding). If the code navigates with history.back(), note that history.back() does not return the previous URL — it only navigates; to keep a usable previous-URL string, explicitly store it (sessionStorage, $rootScope, or intercept $locationChangeStart) or use document.referrer. (docs.angularjs.org)

Notes / troubleshooting:

  • Avoid unescape() — it’s deprecated. (developer.mozilla.org)
  • replace() hacks work but break on edge cases (plus signs, malformed % escapes, other entities); the decode+entity approach above is more reliable. (developer.mozilla.org)

Thanks to and for the trial approaches — prefer the standard decode + entity-decode + strict numeric check shown above.

Recommended Answers

All 2 Replies

I used

a=unescape('Lookups?$filter=VehicleId%20eq%20%272415%27&');
alert(a);

and got back

Lookups?$filter=VehicleId eq '2415'&

Do you need more?

Simply use replace() function with regex. The returned value from window.history.back() is a string. You simply look up for ASCII table and use the "Hex" number. Then add the percentage symbol in the front - i.e. white space is %20. Therefore, your current string "VehicleId%20eq%20%272415%27&" should be "VehicleId eq '2415'&"...

var str = "VehicleId%20eq%20%272415%27&";
var decstr = str.replace(/%20/g, " ");
// decstr -> "VehicleId eq %272415%27&"
decstr = str.replace(/%27/g, "'");
// decstr -> "VehicleId eq '2415'&"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.