EKsumic's Blog

let today = new Beginning();

Click the left button to use the catalog.

OR

' can be used instead of \' escape characters

I have to say that I've been learning .NET Core programming for so long that I only found out today that I didn't know anything about the use of quotation marks in JavaScript.

Original:

<script>
    $(document).ready(function () {
        $("#cancel4").click(function () {
            var ctrl = (navigator.userAgent.toLowerCase()).indexOf('mac') != -1 ? 'Command/Cmd' : 'CTRL';
            if (document.all) {
                window.external.addFavorite('@Html.Raw(ViewBag.CurrentURL)', '@Html.Raw(ViewData["Title"])')
            } else if (window.sidebar) {
                window.sidebar.addPanel('@Html.Raw(ViewData["Title"])', '@Html.Raw(ViewBag.CurrentURL)', "")
            } else {
                alert('You can try the shortcut key' + ctrl + ' + D Add to Favorites~')
            }
        })
    });
</script>

Change to:

<script>
    $(document).ready(function () {
        $("#cancel4").click(function () {
            var ctrl = (navigator.userAgent.toLowerCase()).indexOf('mac') != -1 ? 'Command/Cmd' : 'CTRL';
            if (document.all) {
                window.external.addFavorite('@Html.Raw(ViewBag.CurrentURL)', 'EKsumic\'s Blog')
            } else if (window.sidebar) {
                window.sidebar.addPanel('EKsumic\'s Blog', '@Html.Raw(ViewBag.CurrentURL)', "")
            } else {
                alert('You can try the shortcut key' + ctrl + ' + D Add to Favorites~')
            }
        })
    });
</script>

Yes, the single quotation mark inside the quotation mark must be escaped with a backslash,it's the same way I used to program in C#.

But today I just met this problem. If it wasn't for the English version of my website, I would hardly have met it.

I thought . Net core would help me to automatically escape characters when using ViewData["Title"], but it didn't.

Wait,  I found something wrong with the code.

I found that I used Html.Raw to transmit value.

Maybe Html.Raw is the cause of JavaScript errors.


OK, it's my fault.

If you use ViewData["Title"] instead of @Html.Raw(ViewData["Title"]) , the error will not occur.

At the same time, I found an interesting thing:

&#x27; replaced single quotation mark.

I mean, &#x27; replaced \'.

This is .Net core solution for escaping characters.


So,What is &#x27; ?

Unicode Hex Character Code &#x27;
' Symbol Name: Apostrophe.

This is what Google told me directly.

 

Original:

<script>
    $(document).ready(function () {
        $("#cancel4").click(function () {
            var ctrl = (navigator.userAgent.toLowerCase()).indexOf('mac') != -1 ? 'Command/Cmd' : 'CTRL';
            if (document.all) {
                window.external.addFavorite('https://www.v2know.com/MainPage/PreView/324', 'EKsumic\'s Blog')
            } else if (window.sidebar) {
                window.sidebar.addPanel('EKsumic\'s Blog', 'https://www.v2know.com/MainPage/PreView/324', "")
            } else {
                alert('You can try the shortcut key' + ctrl + ' + D Add to Favorites~')
            }
        })
    });

</script>

.NET Core's ViewData["Title"]

<script>
    $(document).ready(function () {
        $("#cancel4").click(function () {
            var ctrl = (navigator.userAgent.toLowerCase()).indexOf('mac') != -1 ? 'Command/Cmd' : 'CTRL';
            if (document.all) {
                window.external.addFavorite('https://www.v2know.com/MainPage/PreView/252', '[&#x957F;&#x671F;&#x66F4;&#x65B0;]B&#x7AD9;UP&#x4E3B;&#x63A8;&#x8350;&#x5217;&#x8868;&#x2014;&#x2014;&#x5B66;&#x4E60;&#x5411; - EKsumic&#x27;s Blog')
            } else if (window.sidebar) {
                window.sidebar.addPanel('[&#x957F;&#x671F;&#x66F4;&#x65B0;]B&#x7AD9;UP&#x4E3B;&#x63A8;&#x8350;&#x5217;&#x8868;&#x2014;&#x2014;&#x5B66;&#x4E60;&#x5411; - EKsumic&#x27;s Blog', 'https://www.v2know.com/MainPage/PreView/252', "")
            } else {
                alert('You can try the shortcut key' + ctrl + ' + D Add to Favorites~')
            }
        })
    });
</script>

You should find that, in fact, not only &#x27;, almost all characters are encoded in hexadecimal.

In this way, we can find out how . Net core is encoded and how its lifecycle works.

It seems that . Net core passes values first, then runs JavaScript, and in order to prevent escaping characters, it uses hex directly.

This article was last edited at 2020-06-05 20:17:51

* *