开发者

window.open(), browser address bar, and jQuery url decode

I am passing values in a URL query string that are interpreted by JavaScript and used to fill out form elements. The user click a link on one page, is taken to another page which then decodes the values from the URL and populates the form fields.

To decode the URL, I am using the jQuery URL Decoder plugin.

This is the parameter being passed to window.open():

http://mydomain.com/whatever?EmailAddress=me%40privacy.com&YourName=joe%20schmo&CompanyName=TEXAS%20A%20%26%20M%20-%20LUBBOCK%2C%20TX

When I plug that URL into the online version of the decoder, it is properly parsed - the querystring parameters are in the .params object, properly decoded.

However, after the link is clicked when I examine window.location.href, I get this:

http://mydomain.com/whatever?EmailAddress=me@privacy.com&YourName=joe%20schmo&CompanyName=TEXAS%20M%20&%20M开发者_如何学JAVA%20-%20LUBBOCK,%20TX

Which comes out of the URL decoder as a giant mess (i.e. not properly decoded IMO because the input is not properly encoded).

How do I (safely) get back to string that's properly interpreted by the URL decoder?


use a javascript function like this:

function urlencode(str) {
return escape(str)
      .replace(' ', '%20') // or replace with '+'
      .replace('@', '%40');
}

(I know you don't need the first replace but this is more complete... you can add more replaces as you need them or search for a full urlencode)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜