Using 'inside C # line running javascript function

I want to do this:

ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "alert('Vous n'avez pas les droits d'accès à cette application.');", true);

It is impossible to formulate this French sentence without ':

'Vous n'avez pas les droits d'accès à cette application.'

Javascript fails because (possibly) when it encounters this character, it expects it to be the end of the line.

I tried a lot of things like \'and ''', but ... no luck ...

+4
source share
3 answers

JS part

What you want the client to get is a JavaScript string that matters

Vous n'avez pas les droits d'accès à cette application.

To create a string that has this value in JavaScript, the JS string literal must be either:

<Sub> 1sub>
"Vous n'avez pas les droits d'accès à cette application."
^                                                       ^

or

<Sub> 2sub>
'Vous n\'avez pas les droits d\'accès à cette application.'
       ^                      ^

, , .

#

, , JS (. 1 2).

#, , #:

"alert(\"Vous n'avez pas les droits d'accès à cette application.\");"
       ^                                                        ^

@"alert(""Vous n'avez pas les droits d'accès à cette application."");"
^       ^^                                                       ^^

"alert('Vous n\\'avez pas les droits d\\'accès à cette application.');"
              ^^                      ^^

@"alert('Vous n\'avez pas les droits d\'accès à cette application.');"
^              ^                      ^

, , , .

, . , , , //, .

JavaScriptSerializer , .

, , - , . , , . , , , , . [data-*] , ScriptManager , .

, :

var message = "Vous n'avez pas les droits d'accès à cette application.";
var jss = new JavaScriptSerializer();
var encodedMessage = jss.Serialize(message);
ScriptManager.RegisterStartupScript(
  this,
  this.GetType(),
  Guid.NewGuid().ToString(),
  string.Format("alert({0});", encodedMessage),
  true);
+4

:

"alert(\"Vous n'avez pas les droits d'accès à cette application.\");"

?

  • " " . , , JavaScript.
  • , #, , , . . , .

, ( F12, )

alert('Vous n'avez pas les droits d'accès à cette application.');

, "Vous n" , , ; , . .

+11

The result you want for your Javascript is (properly escaped '):

alert('Vous n\'avez pas les droits d\'accès à cette application.');

So you should put \inside your line. Since it \is an escape character, you need to escape from it again to be an alphabetic character \:

ScriptManager.RegisterStartupScript
    ( this
    , this.GetType()
    , Guid.NewGuid().ToString()
    , "alert('Vous n\\'avez pas les droits d\\'accès à cette application.');"
    , true
    );
+2
source

Source: https://habr.com/ru/post/1623435/


All Articles