3

I wanna read an get-parameter from the URL in Javascript. I found this

var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1)),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
};

The problem is, that my paramter is this:

iFZycPLh%Kf27ljF5Hkzp1cEAVR%oUL3$Mce&@XFcdHBb*CRyKkAufgVc32!hUni

I already made urlEncode, so its this:

iFZycPLh%25Kf27ljF5Hkzp1cEAVR%25oUL3%24Mce%26%40XFcdHBb*CRyKkAufgVc32!hUni

But still, if I call the getUrlParameter() function, I just get this as result:

iFZycPLh%Kf27ljF5Hkzp1cEAVR%oUL3$Mce

Does anyone know how I can fix that?

1 Answer 1

4

You need to call decodeURIComponent on sParameterName[0] and sParameterName[1] instead of on the whole of search.substring(1)).

(i.e. on the components of it)

var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = window.location.search.substring(1),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        var key = decodeURIComponent(sParameterName[0]);
        var value = decodeURIComponent(sParameterName[1]);

        if (key === sParam) {
            return value === undefined ? true : value;
        }
    }
};

This is mentioned in zakinster's comment on the answer you link to.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much!! Oh, I didn't read that comment from Zakinster. A mistake on my side. ups

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.