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>
Related
I have a simple HTML page that is attempting to access both remote and local JavaScript files:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<script src="https://js.arcgis.com/3.19/"></script>
<script src="SearchExtent.js"></script>
<script>
require([
...
SearchExtent.js is stored in the same folder as the HTML page.
When debugging the HTML page both in Chrome and Edge, a 404 error occurs indicating that the following resource can't be found:
https://js.arcgis.com/3.19/SearchExtent.js
Why is the browser looking on the remote service instead of the local filesystem>
Dojo must be configured correctly to use both local and CDN sources. Following is the code that correctly loads content from both the CDN and the local server:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<link rel="stylesheet" href="https://js.arcgis.com/3.19/esri/css/esri.css">
<script>
var dojoConfig = {
paths: { js: location.pathname.replace(/\/[^/]+$/, "") + "/js" }
};
</script>
<script src="https://js.arcgis.com/3.19"></script>
<script>
require([
"js/SearchExtent",
"dojo/domReady!"
], function (SearchExtent) {
console.log("...");
});
</script>
</head>
<body>
</body>
</html>
The paths property of dojoConfig is used to specify the location of the "js" alias on the local server. Also note that there is not an explicit reference to the custom JavaScript module. The call to require references the custom module using the alias defined in the paths property.
My GSP file (in Grails 3.1.10):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<asset:javascript src="jquery-2.2.0.min.js"/>
<asset:javascript src="myfile.js"/>
</head>
<body>
<span id="greeting"></span>
</body>
</html>
myfile.js:
greeting = "${resp}"; // resp is passed from controller
$(document).ready(function(){
$('#greeting').val(greeting);
});
Well, I believe in that every grails developer knows if I move myfile.js into my GSP file, it will work.
However, I hope to know how to let the standalone js file can handle the inline variable of GString correctly.
Thanks.
Below is the approach I followed when ran into same problem like yours.
Pass your GString variable to external JS by following way.
Add the below function in your external JS
function view_handler_function(greetingValue){
//assign the value to your element
$('#greeting').val(greetingValue);
.....
//Your other handling code
}
Call your function from your view
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<asset:javascript src="jquery-2.2.0.min.js"/>
<asset:javascript src="myfile.js"/>
</head>
<body>
<span id="greeting"></span>
<script>
var greeting = "${resp}"; // resp is passed from controller
$(document).ready(function(){
//call to your external function
view_handler_function(greeting);
});
</script>
</body>
</html>
Note: This may or may not be the exact answer you are looking for but just one way around I follow.
this is out of the box simply not possible, and it's not a good idea either (although of course you could use a controller action as javascript src and in that action read in the js file and run it's content through a e.g. simpleTemplateEngine)
having js files be interpreted like gstrings/other templates would mean that any caching (bundle files via asset pipeline, cdn distribution and browser caching) had to be disabled.
however, you can simply serve the js files statically and e.g. provide your dynamic input as global variables in inline javascript (e.g. from your layout):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<span id="greeting"></span>
<g:javascript>
var greeting = "${resp}";
</g:javascript>
<asset:javascript src="jquery-2.2.0.min.js"/>
<asset:javascript src="myfile.js"/>
</body>
</html>
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
I am trying to learn how to debug jquery. I tried to make a page which will dynamically add input feilds. The data is sent to the jquery. Now for debugging, I tried to console.log the whole array, but I am getting this error in Firefox:
[17:40:27.073] The character encoding of the HTML document was not
declared. The document will render with garbled text in some browser
configurations if the document contains characters from outside the
US-ASCII range. The character encoding of the page must be declared in
the document or in the transfer protocol. #
file:///Users/ateevchopra/Desktop/takemehome%20dynamic/TakeMeHome/index.html
Please explain what this means of if there is some mistake in my code. Heres my code
HTML:
<!doctype html>
<html>
<head>
<title>TakeMeHome</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.1.custom.min.js"></script>
<script type="text/javascript" src="js/main.js"></script>
<script type='text/javascript' src='js/app.js'></script>
</head>
<body>
<center><form id="details">
Your Place:<input id="source" type="text"><br><br>
Friend1:<input id="friend1" type="text"><br><br>
<div id="friends"></div>
<div id="button">Add!</div><br><br>
<input type="submit" value="go">
</form>
</body>
</html>
jQuery:
var j=2;
var friends = [];
$(document).ready(function(){
$('#button').click(function(){
if(j<11){
$('#friends').append('Friend'+j+':<input type="text" id="friend'+j+'"/><br/><br/>');
j++;
}
else
{
alert("Limit reached");
}
});
});
$("form").submit(function(){
friends[0] = ('#source').val();
for(var i=1;i<j;i++)
{
friends[i] = ('#friends'+i+'').val();
}
console.log(friends);
});
your code is working perfectly you can see it from this
console.log is good for debuging but i prefer you to use firebug for debuging.
Using firebug you can debug each and every line and you can also view the values of each variable.
I am using firebug with firefox.
You can download firebug for firefox from that link .I hope that it helps you.
The error has nothing to do with JavaSCript.
If you add a meta tag like <meta charset="UTF-8" /> it should be fixed.
I also see the you have a type in doctype declaration.
This is not an error in your Javascript code, but a general warning issued by Firefox regarding the validity of the actual HTML markup.
The document's encoding should be declared with a meta tag in inside the header tag. For example, if your encoding is UTF-8 it would be:
<head>
...
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
...
</head>
Since your doctype is HTML5, you can also use the charset attribute:
<head>
...
<meta charset="UTF-8">
...
</head>
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