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:
' replaced single quotation mark.
I mean, ' replaced \'.
This is .Net core solution for escaping characters.
So,What is ' ?
Unicode Hex Character Code '
' 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', '[长期更新]B站UP主推荐列表——学习向 - EKsumic's Blog')
} else if (window.sidebar) {
window.sidebar.addPanel('[长期更新]B站UP主推荐列表——学习向 - EKsumic'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 ', 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.