admin 管理员组

文章数量: 1086019

I use this code from aweber that passes user information from signup to the next page, here is the code from aweber

<script type="text/javascript"> 
    var formData = function(){
        var query_string = (location.search)?((location.search.indexOf('#') != -1) ? location.search.substring(1, location.search.indexOf('#')) : location.search.substring(1)) : '';
        var elements = [];
        if(query_string){
            var pairs = query_string.split("&");
            for(i in pairs) { 
                if (typeof pairs[i] == 'string') {
                    var tmp = pairs[i].split("="); 
                    var queryKey = unescape(tmp[0]);
                    queryKey = (queryKey.charAt(0) == 'c') ? queryKey.replace(/\s/g, "_") : queryKey;elements[queryKey] = unescape(tmp[1]);
                }
            }
        }
        return{display: function(key){if(elements[key]){document.write(elements[key]);
    }else {
        document.write("<!--If desired, replace everything between these quotes with a default in case there is no data in the query string.-->");
    }}}}();
</script>

then this is the code on the page to display name

<script type="text/javascript">formData.display("fullname")</script>

The example output will be FirstName%20LastName

Is there a way that %20 will be removed?

I use this code from aweber that passes user information from signup to the next page, here is the code from aweber

<script type="text/javascript"> 
    var formData = function(){
        var query_string = (location.search)?((location.search.indexOf('#') != -1) ? location.search.substring(1, location.search.indexOf('#')) : location.search.substring(1)) : '';
        var elements = [];
        if(query_string){
            var pairs = query_string.split("&");
            for(i in pairs) { 
                if (typeof pairs[i] == 'string') {
                    var tmp = pairs[i].split("="); 
                    var queryKey = unescape(tmp[0]);
                    queryKey = (queryKey.charAt(0) == 'c') ? queryKey.replace(/\s/g, "_") : queryKey;elements[queryKey] = unescape(tmp[1]);
                }
            }
        }
        return{display: function(key){if(elements[key]){document.write(elements[key]);
    }else {
        document.write("<!--If desired, replace everything between these quotes with a default in case there is no data in the query string.-->");
    }}}}();
</script>

then this is the code on the page to display name

<script type="text/javascript">formData.display("fullname")</script>

The example output will be FirstName%20LastName

Is there a way that %20 will be removed?

Share Improve this question edited May 28, 2014 at 9:10 Tejas Kale 4158 silver badges18 bronze badges asked Feb 21, 2013 at 14:28 tmv1845331tmv1845331 692 silver badges10 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

%20 is the symbol for whitespace in url encoding. For some reason your function returns url-encoded data. Try url decoding the output and your %20 should be represented as whitespace.

You can use decodeURI (http://www.w3schools./jsref/jsref_decodeuri.asp)

EDIT:

Here's a working js-fiddle.

var result = 'Firstname%20Lastname';
var result = decodeURI(result);

document.getElementById('demo').innerHTML = result;

http://jsfiddle/p82HN/

That data is URL-encoded, so use decodeURI or decodeURIComponent to decode it:

var result = decodeURI(input);

Looks like you need urldecode the data, check out decodeURIComponent()

本文标签: javascriptRemoving 20 on outputStack Overflow