Skip to content

Instantly share code, notes, and snippets.

@y-yu
Created June 7, 2012 02:17
Show Gist options
  • Save y-yu/2886113 to your computer and use it in GitHub Desktop.
Save y-yu/2886113 to your computer and use it in GitHub Desktop.
文字化け
var $ = function ( selector, context ) {
context = context || document;
return context.querySelector(selector);
};
var enc = {
s : "Shift_JIS",
e : "EUC-JP",
u : "UTF-8",
i : "ISO-2022-JP"
}[location.hash.substr(1)];
var request = new XMLHttpRequest();
request.open("GET", location.href);
request.setRequestHeader("content-type", "text/html; charset=" + enc);
request.onreadystatechange = function () {
if (request.readyState === 4 && request.status === 200) {
var newSite = document.createElement("html");
newSite.innerHTML = request.response;
var meta = document.createElement("meta");
meta.setAttribute("http-equiv", "Content-Type");
meta.setAttribute("content", "text/html; charset=" + enc);
$("html head", newSite).appendChild(meta);
$("html").removeChild( $("html head"));
$("html").insertBefore($("html head", newSite), document.body);
$("html").removeChild( $("html body"));
$("html").appendChild( $("html body", newSite) );
}
};
request.send();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment