Character encoding changes after window.open()

2019-06-22 07:11发布

问题:

The site from where I'm calling the window.open() function is in UTF-8, using the <meta> tag, everything works well, but once I call the function and open another window with the same tag, the new window shows weird characters even thought in page info it shows that the encoding stays the same (UTF-8).

This is the same problem as mine: http://bytes.com/topic/javascript/answers/622922-character-encoding-after-window-open

回答1:

I had exactly the same problem.

Problem turned out to be that I was setting "Session.CodePage = 65001" in the window opened. That screwed up the character encoding for that session. So when I refreshed the "main" page, some characters (such as åäö) were suddenly garbled.

Solution: make sure the page in the new window doesn't change the session's code page. Good luck!



回答2:

I had the same problem. Just use unescape(decodeURIComponent( YOUR_URL )) inside window.open().

For example: window.open(unescape(decodeURIComponent( YOUR_URL )), '_blank');

Hope it helps.



回答3:

Add a charset="UTF-8" attribute to your javascript tags. This will at least work if the cause of the garbled characters is your included javascript.



回答4:

If the browser uses the same encoding for both pages (which, apparently, it does) then the problem must be in the page itself. It probably contains invalid UTF-8 to begin with.