Charset does not work in Javascript

Asked

Viewed 1,179 times

2

I have the following Javascript function that issues a confirm with a message containing special characters.

function cancelaCadastro(){
    var cancelaCadastro = confirm(unescape("Você deseja cancelar o cadastro e voltar à página inicial?"));
    if (cancelaCadastro == true){
        location.href= "index.php";
    } else{
        return false;
    }
}

The problem is that the message is being displayed as follows. "You want to unsubscribe and return to initial gina?".

I put the unescape to try to correct that mistake, but it was not effective. I inform you that in the HTML page I have already defined the charset for utf-8 and to ISO, but it didn’t solve the problem either. Follows the example of meta-charset:

<meta http-equiv="Content-Type" content="text/html"; charset="UTF-8">

NOTE: This question is not a duplicate of the question Doubt with charset=iso-8859-1 and utf8, because the problem is not related to using 2 different types of charset. Anyway, set the page charset by php using the line of code <?php header('Content-Type: text/html; charset=UTF-8'); ?> and also saved the page as HTML without GOOD, but the problem has not been solved.

  • 2
  • Amigo the answer https://answall.com/a/43205/3635 explains exactly that it is impossible to use 2 Charsets, in fact the answer teaches how to configure a specific single charset, you should not only save the page with "UT8 without BOM", but the files .js also must be saved with "UTF8 without good". Read the answer calmly and will understand, it is a guide to how to do.

  • Guilherme, I saved all files . js in UTF8 without BOM, but it remains the same. I ask you to check the comment I made in Matthew Callixtus' reply. Maybe the information I put in this comment will help identify the problem.

  • In the reply and not in the comment I said that the problem is only to save the . php and . js as UTF-8, I said this is one of the details, have to follow the step by step and do all, It may be that with a header of life already solve, but you will run the results in a different location and in fact you will cause an unimaginable side effect, the best possible is to review all points, header, html, mysql charset, save documents with utf8 including those like include and etc. The answer explains this well. It is not pq a header worked first that you are guaranteed ;)

  • Another thing, you have to see if in fact you want to use UTF-8 or iso-8859-1 and so adjust as desired, if it is iso-8859-1 (or latin1 in the bank) then just exchange utf-8 for iso-8859-1 and save the documents as ANSI, of course set the header. The answer there only used the example utf-8, does not mean that you are required to use UTF-8 ;)

3 answers

1

Add the attribute charset in the script statement. Example:

<script charset="UTF-8"></script>
  • You didn’t solve it, buddy.

  • @Hugoguitti Which editor/IDE are you using to encode? I recommend changing the file encoding in the editor/IDE used and then saving the changes.

  • I am using Sublimetext. The file was saved in UTF-8 without GOOD.

0


Add this code to the PHP index file header;

<?php header("Content-Type: text/html; charset=ISO-8859-1",true) ?>

I await results.

  • I added the proposed code but it didn’t solve the problem. I already saved the file in UTF-8 with BOM and without BOM. I’ve also defined the charset of all scripts for UTF-8. I don’t know if it has anything to do with it, but when I made the PHP code that took the value of page inputs and assigned to a variable, I needed to use "utf8_decode" on variables to ensure that records were made without character errors. Is there any way to give a general code on the page?

  • NOTE: I found that the problem does not occur in Firefox, but occurs in google Chrome.

  • Where are you putting this code I passed, in which file, index.php?

  • I put on the page that I am implementing Javascript, this is a registration page and its extension is . php .

  • I just put the code you passed in index.php and it worked! Thanks a lot Matthew!

  • You are welcome to put in index.php even kkk! anything we are here, you can put my answer as a solution?

  • I just put your answer as a solution, thanks again!

Show 2 more comments

-3

In the CSS: <style> html:lang(pt-br) { @charset "UTF-8"; } </style>

In HTML: <html lang="pt-br">

At the head: <meta charset="UTF-8">

In Javascript - example: <script src="javascript.js" charset="UTF-8"></script>

This way it works when I use the UTF-8 charset in CSS3 along with PHP 7.3, along with Javascript and along with Bootstrap 4.3.1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.