I have a similar problem to this: jQuery AJAX Character Encoding but any solution mentioned there works for me.
I've made three easy files to show the problem:
PHP File:
//prueba.php
echo "nº one two € áéíóú";
JavaScript File (I use JQuery)
//Javascript file
function prueba() {
$.ajax({
type: "GET",
contentType: "application/x-www-form-urlencoded;charset=ISO-8859-1",
url: "prueba.php",
}).done(function( data ) {
$("#prueba").text(data);
//$("#prueba").html(data); //It does the same encoding error
});
}
** HTML File:**
<html>
<head>
<title>E-COMMERCE</title>
<meta content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<script src="javascript/jquery.js" type="text/javascript"></script>
<script src="javascript/javascript.js" type="text/javascript"></script>
</head>
<body>
Prueba
<div id="prueba"></div>
</body>
</html>
And when you click the link Prueba it shows:
Prueba
n� uno dos � �����
The current website works perfectly but it does not use ajax and it is in the same server where i am doing this, so How can I tell to jquery to return ISO-8859-1 instead of whatever it is returning? I know that the ideal is to use always utf-8 but changing to utf-8 it will give us some problems we cant afford right now.
To make the browser use the correct encoding, you have to add an HTTP header to the php page :
header("Content-Type: text/plain; charset=ISO-8859-1");
or you could put the encoding in a meta tag of the html:
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
In your php file you'll need to UTF-8 encode the output:
echo utf8_encode("nº one two € áéíóú");
And then in your html file you will need to set the charset:
<html>
<head>
<meta charset="utf-8"/>
...
And for good practice specify a document type as well:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
...
<!DOCTYPE html> is a legit HTML5 doctype.
And as Pranav Kapoor pointed out maybe you will need to specify the PHP-file charset aswell:
header("Content-Type: text/plain; charset=UTF-8");
I can see you have specified your charset to: ISO-8859-1. I always work with UTF-8 You can read more about it here: What is the difference between UTF-8 and ISO-8859-1?
But remove the contentType from your ajax call
Related
I have developed an app, but when I push the latest version from the app to the Server, the CSS and JS files are not updated. The old files are showing. How can I clear the cache after updating the files?
EDITED like jcubic
Now I use the buster in this way.
index.html:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AIS</title>
<script src="index.php"></script>
<link rel="stylesheet" href="<?= with_hash('style.css') ?>">
</head>
index.php:
<php?
function with_hash($url) {
return $url . "?" . md5(file_get_contents($url));
}
?>
Error message:
Refused to apply style from 'http://127.0.0.1:5500/%3C?=%20with_hash(%27style.css%27)%20?%3E' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
You can use what's called cache buster. Add a unique ID to the URL of the JS and CSS in your application. I personally use the hash of the content of the file, you can get it you write the HTML file in PHP or any other server-side script, but PHP, if you have a single HTML file, is the easiest.
In PHP it can look like this:
<php?
function with_hash($url) {
return $url . "?" . md5(file_get_contents($url));
}
?><!DOCTYPE HTML>
...
<link rel="stylesheet" href="<?= with_hash('produktion.css') ?>">
If the application is for static hosting like Netlify you will need to generate an HTML file with the hash. You can easily do that with Webpack.
I need to feed d3.dsv method with the text file exported from Excel. As to local settings all Excels here would make files with Windows-1251 charset by default.
Is my guess correct that even if I put the right charset value in "init" parameter of d3.dsv method I will not see correctly imported Cyrillic text in console? Unless I use some special recode function for the text received? I used all combinations for charset settings in meta of a html header and in "init" but hasn't succeeded. Is the "init" parameter (which is based on the fetch JS method) useless in this case?
Here's the code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Language" content="ru">
<meta http-equiv="Content-Type" content="text/plain; charset=UTF-8">
<script type="text/javascript" src="d3/d3.js"></script>
</head>
<body>
<script type="text/javascript">
var doc = "raw/test.txt";
var init = {headers: {"Content-type": "text/plain; charset=Windows-1251"}};
d3.dsv("\t", doc, init, ).then(function(data) {
console.log(data);
});
</script>
</body>
</html>
<!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.
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
My page is located here
As you can see, it doesn't look like it's working properly. This becomes very apparent if you try to open it in an older browser (this is what initially set me off).
I checked the interpreted source code from chrome and it shows me the following:
<html>
<head>
<style type="text/css"></style>
</head>
<body>
<title>Internet adgang - Hurtig opsætning - Ansatte</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
Etc..
Something seems to be closing my head tag which is causing all SORTS of errors:
W3 validator
I have absolutely no idea what is causing this.
You can see my source code here (I've scrambled the PHP):
The code above does not contain my body code, as I am fairly sure that has nothing to do with the head tag closing all of a sudden.
Here is some source of a page that works just fine:
I am completely baffled by this and I have no idea what to do nor what is causing it.
Any ideas?
Solution
With the help of Buttc4k3 and vogomatix I finally found the solution. As buttc4k3 said, there was an illegal "zero-width no-space" character hidden in the file which didn't show in my sublime text editor. It would only show if you opened the file in Notepad++. I could not understand why this character would appear in google chrome, and not in my source, but after googling it, I found that a file saved in UTF-8 WITH BOM (Byte Order Marking) would save the BOM as a zero-width no-space character! This was where vogomatix saved me and told me that my file was saved with BOM and after recoding it without BOM it works fine.
So - if you have the same problem as me - save your file WITHOUT BOM ENCODING (this can be done in notepad++).
I took the page source of your website and pasted it in Notepad++ and found a zero-width no-break space in line 3 column 5. It renders invisible in most editors/viewers, but it breaks the HTML parser of the browser. I don't know how or why it got there since I can't find it in the PHP code, but maybe you can find it by opening your code in Notepad++. It renders this character as a tiny dot. If you can't find it, delete everything from and including <head> to <title> and re-type it.
I hope this works.
PS: Even though it is not the reason why your page is broken, you should add <html> and </html> to the markup. Most browsers can deal with its absence, but it is there for a reason.
It seems like you are missing the <html> tag.
Current HTML-Code:
<!DOCTYPE html>
<head>
<title>Internet adgang - Hurtig opsætning - Ansatte</title>
Must be something like that:
<!DOCTYPE html>
<html>
<head>
<title>Internet adgang - Hurtig opsætning - Ansatte</title>
[...]
</head>
<body>
[...]
</body>
</html>
Edit the first part of your site to be as follows:
<?php
include 'XXXXX';
include 'XXXXX';
if(!isset($_GET['lang'])) {
$lang = "DA";
} else {
$lang = $_GET['lang'];
}
?>
<!DOCTYPE html>
<html lang="<?php $lang; ?>">
<head>
<title><?php echo trans("Internet adgang - Hurtig opsætning - Ansatte", $lang); ?></title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<link rel="stylesheet" href="resources/styles/style.css" media="all">
<script src="resources/js/jquery.min.js" type="text/javascript"></script>
<script src="resources/js/jquery-ui.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="resources/styles/au-flowbox-style.css">
<script type="text/javascript" src="resources/js/au-flowbox.js"></script>
<script type="text/javascript" src="resources/js/au-flowbox.data.js"></script>
</head>
<body>
....