HTML Javascript displaying Japanese characters incorrectly? - javascript

My HTML
<head >
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<div id="Target">食べる</div>
This displays perfectly fine .
document.getElementById("Target").innerHTML = "会う";
But once I use javascript to change the value of "Target" I get ��.
I'm fairly inexperienced when it comes to both javascript and encodings, so I apologize in advance if this is an easy fix.

Related

How to live reload the changes to my file.html?

I've been using livereload with sublime text for a while, in order to live reload markdown and preview in the browser.
Now I'm wondering if it's possible to live reload non markdown also, i.e. my file.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
hello world 4
<script>document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1"></' + 'script>')</script>
</body>
</html>
So I was hoping to save that file on sublime text and see the changes on the browser, but that's not happening. Perhaps I'm missing some fundamentals on how things work. Is that supposed to work, what part am I doing wrong?

Javascript escape quote in a function

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>untitled</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function sendToCart(a,b,c,d,f){
alert(arguments[1].replace(/'/g, "\\'"))
}
</script>
</head>
<body>
send to chart
</body>
</html>
can anyone please tell me why alert is not working? It is working fine when I directly escape while passing value at the time of onclick but not working after reading from function?
You will need to fix the server-side code to make sure you escape apostrophes. On server-side, depending on your server, you should have good means to escape apostrophes. If you are using PHP, you might want to call addslashes. Or you can call a method in your server-side which replaces apostrophe with \\'
The error occurs when the JavaScript parser reaches the s after the string literal 'Lover'.
Your escape function runs later (or would, if the syntax error hadn't stopped further execution).
You can't fix a syntax error in your JavaScript source code by trying to apply regular expressions to it, from inside the same program, after the parse error has already been hit.

How to construct a document from a string

How to construct a document from a string
I have got a string, which is html-like ,I want to extract the element in the html text, I know that I can use htmlparser with java, but how to do the same function with javascript?
How can I construct a document from the string, Does createHTMLDocument work?
Or any other way to extract the element in the html text?
for example:
I have got the html text as :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>titleValue</title>
<meta name="description" content="It is a good way to learn science." />
<meta name="keywords" content="Symfony2,Redis,PHP" />
<meta name="author" content="CSDN.NET" />
<meta name="Copyright" content="CSDN.NET" />
</head>
<body>
..........................
</body>
</html>
how to get the value of "description"
Here is my code, but the output is 0, what's wrong?
var el = document.createElement("div");
el.innerHTML = ' <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>titleValue</title> <meta name="description" content="It is a good way to learn science." /> <meta name="keywords" content="Symfony2,Redis,PHP" /> <meta name="author" content="CSDN.NET" /> <meta name="Copyright" content="CSDN.NET" /> </head> <body> hello</body> </html>';
var descElements = el.getElementsByTagName("head");
document.getElementById("news_content").innerHTML = descElements.length;
The simplest way to do this kind of manipulation would be with a library like jQuery. This is one way you could accomplish this task with jQuery (see a demo):
var markup = '<!DOCTYPE ...';
var parsed = $(markup);
var description = parsed.filter("meta[name='description']").attr('content');
alert(description);
Note that you will not have access to all elements (the <head/> element is not represented, for example) because not all elements are legal in the context of another document. The <meta/> elements should be fine, though.

How do I set the character encoding of a child window

I'm opening a child window and writing contents into it in JavaScript. The parent page has UTF-8 an the character set, but the child window opens with ISO-8859-1
I've tried using document.write to set the child page content type using the meta tag, but it appears to have no effect.
launcherHtml = '
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
</body>
</html>
'
sandbox = window.open()
sandbox.document.open()
sandbox.document.write(launcherHtml)
sandbox.document.close()
sandbox.document.characterSet // ISO-8859-1 !!!
I've also tried using <meta charset="UTF-8"> as the tag in the child window but the results are the same.
How do I properly specify the charset of a popup window as UTF-8?
A method from late 2016 that works for multiple mime types including "text/plain" and "text/html":
use a data URL,
specify mime-type
specify character encoding as UTF-8.
convert text to escaped UTF-8 characters using uriEncode
replace '#' in the UTF octet string with "%23".
JavaScript example :
var launcherHtml = `
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>UTF-8 example</title>
</head>
<body>
<h1>Example using unicode and '#'</h1>
Hello folks, 你好! (nǐ hǎo) in Chinese, good evening,
こんばんは (in Japanese).
</body>
</html>
`;
var launcherURL = "data:text/html;charset=utf-8,"
+ encodeURI( launcherHtml).replace(/#/g,"%23");
sandbox = window.open(launcherURL)
sandbox.document.characterSet // UTF-8, yippee

jQuery load() encoding for Korean (and Japanese) lang

I've recently ran into a problem and I would really use a bit of help from the community. I'm trying to load a HTML file using jQuery load().
The index file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Question</title>
<meta http-equiv="Content-Type" Content="text/html; charset=euc-kr" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
function restart(){
$("#wrap").load('new-content.html').hide().fadeIn('slow');
}
</script>
</head>
<body>
<div id="wrap">
<div id="content">Text before restart: 남성</div>
</div>
<br />
Restart
</body>
</html>
The new HTML file includes some simple HTML content and a call to a .js file (used for some animations). The new content file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Question</title>
<meta http-equiv="Content-Type" Content="text/html; charset=euc-kr" />
<script src="main.js" language="javascript" type="text/javascript" />
</head>
<body>
<div id="contentInner">Text after restart: 여성</div>
</body>
</html>
As you can see I've included the correct encoding charsets (for both files) and I've double checked and I can confirm that every file used for this part of the code it's encoded as "KOREAN" (checked in different apps like Dreamweaver, Notepad++, Sublime Text 2).
Yet still once I load the content the characters are not encoded properly; question marks appearing instead of characters.
Of course I made some searching before posting and I found a few helpful topics about this called the PHP header solution, but this isn't an options since it need to use HTML/JS/CSS only.
The same behavior occurs when I'm using the Japanese language too. In other latin based languages everything is working perfectly fine of course.
Any sort of input/advice/help would be much much appreciated.
Thank you!

Categories