Problems when displaying javascript string

Asked

Viewed 145 times

2

I have the following code:

$(function(){
    var curso = controller.getNomeCurso();
    $("#curso").text(curso);
});

The problem is in line 18, because I cannot display values with special characters correctly.

Example: When sending Nutrição, the displayed value is Nutri%C3%A7%C3%A3o, right in the line 4 existing a meta tag defining the charset as UTF-8.

How can I fix this?

3 answers

1


Cara modified the controller function with the hint of @Marcusvinicius.

var controller = {
    getNomeCurso : function() {
        return window.decodeURIComponent(itens[0]); <-- window.decodeURIComponent("variavel para codificar")

    },
    voltar: function(){
        location.href = "turmas.html";
    }
};

1

try like this:

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  • Thanks for the help, but unfortunately it didn’t work.

  • No coding problem, special characters are encoded and not corrupted.

  • Add charset to your external js: <script type="text/javascript" src="*. js" charset="utf-8"></script>

  • Also try changing the charset property to iso-8859-1

  • Thank you very much for the help, but unfortunately none of the options worked. I also did not intend to get the hint to put the charset to my external js.

  • is to add the charset="utf-8" or charset="iso-8859-1" property to your external js link, so when the page prompts js, it would recognize the encoding.

  • Guy opened on IE and it was normally, but on Chrome I came across the coding problem, I’ll be one thing and let you know if I’m positive.

Show 2 more comments

1

Apparently, the string is encoded. If you use the function encodeURIComponent in string "Nutrition" will get "Nutri%C3%A7%C3%A3o".

Then, to correct, use:

var curso = window.decodeURIComponent(controller.getNomeCurso());
$("#curso").text(curso);

var curso = window.decodeURIComponent("Nutri%C3%A7%C3%A3o");
$("#curso").text(curso);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="curso"></div>

  • Thanks for the help, but unfortunately it didn’t work.

  • I had put the wrong code, edited my answer. Try again with the code: var curso = window.decodeURIComponent(controller.getNomeCurso());

Browser other questions tagged

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