The charset
property, which can only be used
for external scripts,
sets or gets the charset attribute of a script.
The charset attribute from script element sets the character encoding used in an external script file.
The charset
attribute is used when the character encoding in an external
script file differs from the encoding in the HTML document.
charset |
Yes | Yes | Yes | Yes | Yes |
Return the charset property:
scriptObject.charset
Set the charset property:
scriptObject.charset=charset
Value | Description |
---|---|
charset | Set the character encoding of an external script file. for example, "ISO-8859-1" or "UTF-8" |
A String type value represents the character encoding value.
The following code shows how to get the character encoding value.
<!DOCTYPE html>
<html>
<body>
<script id="myScript" src="demo_.js" charset="UTF-8"></script>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--from w ww .java 2s . c o m-->
var x = document.getElementById("myScript").charset;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
The code above is rendered as follows: