最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Javascript - encodeURI() returing different results in different browsers - Stack Overflow

programmeradmin3浏览0评论

I use encodeURI() to encode a php request url, it works perfectly fine in Firefox and Chrome but it doesn't in IE/Edge.

The actual url is: http://localhost/get.php?id=e_e2&title=e2&desc=just a note&stime=6/12/2015, 1:00:00 AM&etime=15/12/2015, 1:00:00 PM

What Firefox returns is (works): http://localhost/get.php?id=e_HO%20Event%201&title=HO%20Event%201&desc=This%20is%20just%20a%20test%20event%20of%20this%20handover%20only&stime=6/12/2015,%201:00:00%20AM&etime=10/12/2015,%201:00:00%20PM

What IE returns (Break the php code): http://localhost/get.php?id=e_HO%20Event%201&title=HO%20Event%201&desc=This%20is%20just%20a%20test%20event%20of%20this%20handover%20only&stime=%E2%80%8E6%E2%80%8E/%E2%80%8E12%E2%80%8E/%E2%80%8E2015%E2%80%8E%20%E2%80%8E1%E2%80%8E:%E2%80%8E00%E2%80%8E:%E2%80%8E00%E2%80%8E%20%E2%80%8EAM&etime=%E2%80%8E10%E2%80%8E/%E2%80%8E12%E2%80%8E/%E2%80%8E2015%E2%80%8E%20%E2%80%8E1%E2%80%8E:%E2%80%8E00%E2%80%8E:%E2%80%8E00%E2%80%8E%20%E2%80%8EPM

I have tried to decode what IE returns but it caused me a lots of problems!, so is there an alternatives to encodeURI() ?, FF seems to work even if I dont encode the url, where IE works if I copy the FF encoded url to it!


update: example code link

I think it has something to do with toLocaleString()


Final update:

As few answered, it was some weard marks in the link "appears only in IE!" I had to filter and changes what my php script date format to remove the ma

function FixLocaleDateString(localeDate) {
    var newStr = "";
    for (var i = 0; i < localeDate.length; i++) {
        var code = localeDate.charCodeAt(i);
            if (code != 44 && code != 8206 ) {
                newStr += localeDate.charAt(i);
            }
    }
        return newStr;
}

I found this function in another answer and modify it: ToLocaleDateString() changes in IE11

I use encodeURI() to encode a php request url, it works perfectly fine in Firefox and Chrome but it doesn't in IE/Edge.

The actual url is: http://localhost/get.php?id=e_e2&title=e2&desc=just a note&stime=6/12/2015, 1:00:00 AM&etime=15/12/2015, 1:00:00 PM

What Firefox returns is (works): http://localhost/get.php?id=e_HO%20Event%201&title=HO%20Event%201&desc=This%20is%20just%20a%20test%20event%20of%20this%20handover%20only&stime=6/12/2015,%201:00:00%20AM&etime=10/12/2015,%201:00:00%20PM

What IE returns (Break the php code): http://localhost/get.php?id=e_HO%20Event%201&title=HO%20Event%201&desc=This%20is%20just%20a%20test%20event%20of%20this%20handover%20only&stime=%E2%80%8E6%E2%80%8E/%E2%80%8E12%E2%80%8E/%E2%80%8E2015%E2%80%8E%20%E2%80%8E1%E2%80%8E:%E2%80%8E00%E2%80%8E:%E2%80%8E00%E2%80%8E%20%E2%80%8EAM&etime=%E2%80%8E10%E2%80%8E/%E2%80%8E12%E2%80%8E/%E2%80%8E2015%E2%80%8E%20%E2%80%8E1%E2%80%8E:%E2%80%8E00%E2%80%8E:%E2%80%8E00%E2%80%8E%20%E2%80%8EPM

I have tried to decode what IE returns but it caused me a lots of problems!, so is there an alternatives to encodeURI() ?, FF seems to work even if I dont encode the url, where IE works if I copy the FF encoded url to it!


update: example code link

I think it has something to do with toLocaleString()


Final update:

As few answered, it was some weard marks in the link "appears only in IE!" I had to filter and changes what my php script date format to remove the ma

function FixLocaleDateString(localeDate) {
    var newStr = "";
    for (var i = 0; i < localeDate.length; i++) {
        var code = localeDate.charCodeAt(i);
            if (code != 44 && code != 8206 ) {
                newStr += localeDate.charAt(i);
            }
    }
        return newStr;
}

I found this function in another answer and modify it: ToLocaleDateString() changes in IE11

Share Improve this question edited May 23, 2017 at 12:04 CommunityBot 11 silver badge asked Dec 13, 2015 at 9:27 et3rnalet3rnal 3424 silver badges18 bronze badges 3
  • Try encodeURIComponent() for special characters – SyntaxLAMP Commented Dec 13, 2015 at 9:31
  • Please show your actual code. – Max Zuber Commented Dec 13, 2015 at 9:31
  • @SyntaxLAMP not, it will mess up the whole url and will not work anywhere @MaxZuber this is the code var phphref = 'http://localhost/get.php?id=e_'+event.title+'&title='+event.title+'&desc='+event.description+'&stime='+stime+'&etime='+etime; console.log('url only:'); console.log(phphref); //fine function myFunction(uri) { var res = encodeURI(uri); //console.log(res); return res; } var calfile = myFunction(phphref); console.log('encoded:'); console.log(calfile); //fine in ff, not in ie ! thanks – et3rnal Commented Dec 13, 2015 at 10:36
Add a ment  | 

3 Answers 3

Reset to default 5

The problem is not encodeURI (though as others have pointed out, you should never use encodeURI, but rather encode individual ponents with encodeURIComponent and then join them together).

The problem is that the dates for some reason contains lots of U+200E "LEFT-TO-RIGHT MARK" characters (which are invisible, but still present), which once encoded bee %E2%80%8E.

Show us where/how you get the times, or filter the time strings to remove those characters before encoding.

Define a simple params object to represent desired URL parameters and glue its items. If there some incorrect characters in the properties, you need to filter them out before encodeURIComponent() call.

var params = {
  'id': 'e_'.concat(event.title.toString()),
  'title': event.title.toString(),
  'desc': event.description.toString(),
  'stime': stime.toString(),
  'etime': etime.toString()
};

var chunks = [];
for (var property in params) {
  if (params.hasOwnProperty(property)) {
    chunks.push(property.concat('=', encodeURIComponent(params[property])));
  }
}
var href = 'http://localhost/get.php?'.concat(chunks.join('&'));

console.log(href);

Use encodeURIComponent when you want to encode a URL parameter.

Here is some useful links

When are you supposed to use escape instead of encodeURI / encodeURIComponent?

https://coderwall./p/y347ug/encodeuri-vs-encodeuriponent

发布评论

评论列表(0)

  1. 暂无评论