简体   繁体   English

将原始 HTML 标记分配给 JavaScript 变量

[英]Assign a raw HTML tag to a JavaScript variable

I'm working on an existing ASP.Net application and came across an interesting piece of JavaScript that I've been wondering about.我正在处理一个现有的 ASP.Net 应用程序,并遇到了一段我一直想知道的有趣的 JavaScript。

A few variables are being declared as literals, and they aren't in strings.一些变量被声明为文字,它们不在字符串中。 For example, something like this is done:例如,完成了这样的事情:

<script type="text/javascript">  
    var jsonData = <asp:Literal ID="MyJsonObject" runat="server" />;
    ......
</script>

And in the server side code(C#), these tags are actually being altered from a JsonTextWriter similarly to this:在服务器端代码(C#)中,这些标签实际上是从JsonTextWriter中改变的,类似于:

var stringBuilder = new StringBuilder();
var stringWriter = new StringWriter(stringBuilder);

using (var jsonWriter = new JsonTextWriter(stringWriter))
{
    jsonWriter.Formatting = Formatting.Indented;

    jsonWriter.WriteStartObject();
    jsonWriter.WritePropertyName("someProperty");
    jsonWriter.WriteValue("someValue");
    jsonWriter.WriteEndObject();
}

MyJsonObject.Value = stringBuilder.ToString();

This in turn is causing the jsonData variable to be able to be used as a Json object in the client side code.这反过来又导致 jsonData 变量能够在客户端代码中用作 Json 对象。

What I've noticed is, if I put single quotes around the tag and change it to:我注意到的是,如果我在标签周围加上单引号并将其更改为:

<script type="text/javascript">  
    var jsonData = '<asp:Literal ID="MyJsonObject" runat="server" />';
    ......
</script>

This doesn't work as it's no longer coming across as a Json object, but as an actual string.这不起作用,因为它不再是一个 Json 对象,而是一个实际的字符串。 However, as I would expect, when it's just a plain tag, I'm receiving several syntax errors from Visual Studio.但是,正如我所料,当它只是一个普通标签时,我会收到来自 Visual Studio 的几个语法错误。

So my question is:所以我的问题是:

Is this something that's generally acceptable or should I avoid keeping things like this around?这是普遍可以接受的东西还是我应该避免保留这样的东西? Yeah, it works , but it doesn't look right to me.是的,它有效,但在我看来不合适。

I've found a few different questions that seem to be related here, but none seem to provide any insight on my particular situation.我发现了一些似乎与此处相关的不同问题,但似乎没有一个问题可以提供有关我的特定情况的任何见解。

In short: Yes, this looks acceptable.简而言之:是的,这看起来可以接受。 That appears to be one of the intended use-cases of the <asp:Literal> tag.这似乎是<asp:Literal>标记的预期用例之一。 Since you're writing a Javascript object literal, you won't even need the single-quotes, since that will change how the code works.由于您正在编写 Javascript 对象字面量,因此您甚至不需要单引号,因为这会改变代码的工作方式。

The thing to note is that <asp:Literal> isn't an HTML tag , and therefore will not make it to the user as it is.需要注意的是<asp:Literal>不是 HTML 标记,因此不会按原样提供给用户。 The <asp:Literal> tag gets pre-processed by ASP.NET before the page is sent to the user (since you have runat="server" specified); <asp:Literal>标记在页面发送给用户之前由 ASP.NET 进行预处理(因为您指定了runat="server" ); therefore, it will be processed regardless of surrounding content and replaced entirely with the resolved string value.因此,无论周围的内容如何,​​它都会被处理并完全替换为解析的字符串值。 Thus, your rendered page will be transformed (on the server-side) from this:因此,您呈现的页面将由此转换(在服务器端):

var jsonData = <asp:Literal ID="MyJsonObject" runat="server" />;

to this:对此:

var jsonData = {
    "someProperty" : "someValue"
};

This is a good way of transferring rich data constructed on the server-side into the client-side Javascript without having to write out raw Javascript to the page.这是将在服务器端构建的丰富数据传输到客户端 Javascript 的好方法,而无需将原始 Javascript 写出到页面。

If you wanted a JSON-formatted string instead, then doing what you're doing and calling JSON.stringify(jsonData) afterwards would be your best bet.如果你想要一个 JSON 格式的字符串,那么做你正在做的事情并在之后调用JSON.stringify(jsonData)将是你最好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM