包含JavaScript转义字符的字符串的问题 [英] Problem With String Containing JavaScript Escape Character

查看:83
本文介绍了包含JavaScript转义字符的字符串的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个VB.NET代码隐藏模块中,我为一个指向

JavaScript函数的链接构建了一个字符串。下面的两行代码显示了相关内容。


PopupLink =" javascript :PopUpWindow(& Chr(34)& PopUpWindowTitle& ; Chr(34)

&","& Chr(34)& CurrentEventDetails&")"

strTemp + ="< ; BR>< A HREF =''#''onClick =''" &安培; PopupLink& ">" &安培; EventName&

"< / A>< BR>"


我遇到的问题是当字符串变量[PopUpWindowTitle]或

[CurrentEventDetails]包含带撇号的字符串,然后服务器

抛出错误未终止的字符串常量。这个错误出现在

之前,浏览器中的页面呈现(或者当页面呈现给

浏览器时 - 我不知道哪个 - 但它在页面显示在

浏览器之前。


我试过包含JavaScript的'\'"转义字符 - 但这不会解决问题(例如,PopUpWindowTitle = Replace(PopUpWindowTitle,"''",

" \''"服务器仍会抛出Unterminated String Constant错误。


我能做些什么呢?我需要在JavaScript中包含撇号

函数调用字符串并且VB.NET忽略了JavaScript的Escape

字符 - 遵循JavaScript的语法规则。


提前致谢。

In a VB.NET code behind module, I build a string for a link that points to a
JavaScript function. The two lines of code below show what is relevant.

PopupLink = "javascript:PopUpWindow(" & Chr(34) & PopUpWindowTitle & Chr(34)
& ", " & Chr(34) & CurrentEventDetails & ")"
strTemp += "<BR><A HREF=''#'' onClick=''" & PopupLink & "''>" & EventName &
"</A><BR>"

The problem I have is that when the string variables [PopUpWindowTitle] or
[CurrentEventDetails] contain a string with an apostrophe, then the server
throws an error "Unterminated String Constant". This error shows up prior to
the page rendering in the browser (or as the page is being rendered to the
browser - I''m not sure which - but it''s before the page shows up in the
browser).

I tried including JavaScript''s "\" escape character - but this does not
solve the problem (e.g., PopUpWindowTitle= Replace(PopUpWindowTitle, "''",
"\'' "). The server still throws the Unterminated String Constant error.

What can I do about this? I need to include apostrophes in the JavaScript
function call string and VB.NET is ignoring JavaScript''s Escape
character -yet honoring JavaScript''s syntax rules.

Thanks in advance.

推荐答案

Jeff S写道:
在一个VB.NET代码隐藏模块中,我为一个指向<的链接构建了一个字符串br /> JavaScript函数。下面的两行代码显示了相关的内容。

PopupLink =" javascript :PopUpWindow(& Chr(34)& PopUpWindowTitle& Chr(34)
&","& Chr(34)& CurrentEventDetails&")"
strTemp + =&quo t;< BR>< A HREF =''#''onClick =''" &安培; PopupLink& ">" &安培; EventName&
"< / A>< BR>"

我遇到的问题是当字符串变量[PopUpWindowTitle]或
[CurrentEventDetails]包含时带有撇号的字符串,然后服务器会抛出错误未终止的字符串常量。这个错误出现在浏览器中的页面呈现之前(或者当页面呈现给
浏览器时 - 我不确定是哪一个 - 但它是在页面显示之前)在
浏览器中。

我尝试包含JavaScript的\和转义字符 - 但这并不能解决问题(例如,PopUpWindowTitle = Replace(PopUpWindowTitle,"'",
" \''")。服务器仍会抛出未终止状态字符串常量错误。

我该怎么办呢?我需要在JavaScript
函数调用字符串中包含撇号,而VB.NET忽略了JavaScript的Escape
字符-yet表示尊重JavaScript的语法规则。

提前致谢。
In a VB.NET code behind module, I build a string for a link that points to a
JavaScript function. The two lines of code below show what is relevant.

PopupLink = "javascript:PopUpWindow(" & Chr(34) & PopUpWindowTitle & Chr(34)
& ", " & Chr(34) & CurrentEventDetails & ")"
strTemp += "<BR><A HREF=''#'' onClick=''" & PopupLink & "''>" & EventName &
"</A><BR>"

The problem I have is that when the string variables [PopUpWindowTitle] or
[CurrentEventDetails] contain a string with an apostrophe, then the server
throws an error "Unterminated String Constant". This error shows up prior to
the page rendering in the browser (or as the page is being rendered to the
browser - I''m not sure which - but it''s before the page shows up in the
browser).

I tried including JavaScript''s "\" escape character - but this does not
solve the problem (e.g., PopUpWindowTitle= Replace(PopUpWindowTitle, "''",
"\'' "). The server still throws the Unterminated String Constant error.

What can I do about this? I need to include apostrophes in the JavaScript
function call string and VB.NET is ignoring JavaScript''s Escape
character -yet honoring JavaScript''s syntax rules.

Thanks in advance.




你真的得到了错误吗?服务器,而不是客户?


我可以看到几个可能的问题:


1)在你的2行样本中,你遗失了构建PopupLink的

表达式中的一个Chr(34)项。似乎那个

表达式的结尾应该是:


CurrentEventDetails& Chr(34)& )"


not:


CurrentEventDetails& )"


,因为你可能需要为客户端PopUpWindow()函数提供

CurrentEventDetails参数的结束双引号。


我愿意打赌丢失的Chr(34)表达式在你的

代码中,它可能只是你帖子中的错字 - < ;肥皂盒>我说的是,

cut-n-paste是你的朋友。在代码中输入拼写错误是不可原谅的。

片段可以从真实代码中粘贴。我的意思是,如果你的代码片段中有一个

错误,应该证明这个问题,那么读者应该知道问题到底是什么? < / soapbox>


2)这是我认为是你真正的问题。而不是:


替换(PopUpWindowTitle,"''"," \''")


尝试使用:


替换(PopUpWindowTitle,"''"," \ x27")


或:


替换(PopUpWindowTitle,"''","'")

浏览器不会对转义序列进行JavaScript式处理 -

它使用Character Entities来转义特殊字符。并且

浏览器显然需要先处理页面,然后才能将页面的一些内容传递给JavaScript处理器。


如果用\ xx27替换撇号,那么该字符串意味着

浏览器没有任何内容,因此传入JavaScript的内容是

处理器,将其视为撇号的转义序列。


如果替换为',则该字符串为''字符

实体''浏览器在传递之前转换为撇号

将字符串输入到JavaScript处理器。


很抱歉这篇长篇文章。

-

mikeb



Are you really getting the error on the server, not the client?

I can see a couple possible problems:

1) in your 2 line sample above, you are missing a Chr(34) term in the
expression that builds PopupLink. it seems that the end of that
expression should be:

CurrentEventDetails & Chr(34) & ")"

not:

CurrentEventDetails & ")"

since you presumably need a closing double quote for the
CurrentEventDetails parameter to the client side PopUpWindow() function.

I''d be willing to bet that the missing Chr(34) expression is in your
code, it''s probably just a typo in your post - <soapbox> to which I say,
"cut-n-paste is your friend". It''s inexcusable to have typos in code
snippets that can be pasted from the real code. I mean, if you have a
typo in your snippet that is supposed to demonstrate the problem, how
are the readers supposed to know what the problem really is? </soapbox>

2) this is what I believe to be your real problem. Instead of:

Replace(PopUpWindowTitle, "''","\'' ")

try using:

Replace(PopUpWindowTitle, "''","\x27")

or:

Replace(PopUpWindowTitle, "''","'")
The browser doesn''t do JavaScript-style handling of escape sequences -
it uses Character Entities to escape special characters. And the
browser obviously needs to process the page before it can pass bits of
the page off to the JavaScript processor.

If you replace apostrophes with "\x27", that string of characters means
nothing to the browser, so that''s what gets passed in to the JavaScript
processor, which sees it as an escape sequence for the apostrophe.

If you replace with "'", that string of characters is an ''Character
Entity'' which the browser translates into an apostrophe before passing
the string on to the JavaScript processor.

Sorry for the long post.
--
mikeb


response.write(" this is a double"" ;引用")


输出=这是一个双倍的引用


" Jeff S" <济*** @ asdf.net>在消息中写道

新闻:************** @ tk2msftngp13.phx.gbl ...
response.write ("this is a double "" quote")

output = this is a double " quote


"Jeff S" <Je***@asdf.net> wrote in message
news:Of**************@tk2msftngp13.phx.gbl...
在VB.NET中代码隐藏模块,我为一个指向
a JavaScript函数的链接构建了一个字符串。下面的两行代码显示了相关内容。

PopupLink =" javascript :PopUpWindow(& Chr(34)& PopUpWindowTitle&
Chr(34)&","& Chr(34)& CurrentEventDetails&")"
strTemp + ="< BR>< A HREF =''#' 'onClick =''" &安培; PopupLink& ">" &安培; EventName&
"< / A>< BR>"

我遇到的问题是当字符串变量[PopUpWindowTitle]或
[CurrentEventDetails]包含时带有撇号的字符串,然后服务器会抛出错误未终止的字符串常量。此错误显示在浏览器中的页面呈现之前
(或者当页面呈现给
浏览器时 - 我不确定哪个 - 但是在页面显示之前在
浏览器中。

我尝试包含JavaScript的\和转义字符 - 但这并不能解决问题(例如,PopUpWindowTitle = Replace(PopUpWindowTitle,"'",
" \''")。服务器仍会抛出未终止状态字符串常量错误。

我该怎么办呢?我需要在JavaScript
函数调用字符串中包含撇号,而VB.NET忽略了JavaScript的Escape
字符-yet表示尊重JavaScript的语法规则。

提前致谢。
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant.

PopupLink = "javascript:PopUpWindow(" & Chr(34) & PopUpWindowTitle & Chr(34) & ", " & Chr(34) & CurrentEventDetails & ")"
strTemp += "<BR><A HREF=''#'' onClick=''" & PopupLink & "''>" & EventName &
"</A><BR>"

The problem I have is that when the string variables [PopUpWindowTitle] or
[CurrentEventDetails] contain a string with an apostrophe, then the server
throws an error "Unterminated String Constant". This error shows up prior to the page rendering in the browser (or as the page is being rendered to the
browser - I''m not sure which - but it''s before the page shows up in the
browser).

I tried including JavaScript''s "\" escape character - but this does not
solve the problem (e.g., PopUpWindowTitle= Replace(PopUpWindowTitle, "''",
"\'' "). The server still throws the Unterminated String Constant error.

What can I do about this? I need to include apostrophes in the JavaScript
function call string and VB.NET is ignoring JavaScript''s Escape
character -yet honoring JavaScript''s syntax rules.

Thanks in advance.



显然你看到了我的小伎俩将更简单的代码发布到

新闻组,而不是我实际使用的内容(以及你正确结束的遗漏/错误

)。你的< soapbox>积分很好......


我继续提出你的建议,但我仍然得到错误。所以,我

发布了完整的部分代码如下。


此外,仔细观察,序列为t帽子导致错误

消息出现如下:

1.在VS.NET IDE中,我按F5运行项目(在ASPX页面中

问题设置为起始页面。

2.浏览器窗口出现,我看到ASPX页面的顶部

呈现(例如,页面标题图形显示)。

3.出现消息框,显示错误:未终止字符串

常量。消息。

4.此时,只显示页面的顶部,并在浏览器窗口中间显示消息

框。如果我点击

是或否(消息框中只有两个按钮 - 提示调试或

没有),页面确实在浏览器中完成渲染。页面看起来很好,

但链接已经死了(大概是因为JavaScript函数调用部分

的链接无效)。


这里是代码(后跟运行时样本值):


每个CalendarEntry在m_CalendarEntries中

如果CalendarEntry.EventDateTime = DateToGet那么


''准备好JS弹出窗口

CurrentDate = CStr(CalendarEntry.EventDateTime)

CurrentEventDetails = CalendarEntry.LongDescription

CurrentEventContact = CalendarEntry.ContactName

CurrentEventContactEmail = CalendarEntry.ContactEMail

CurrentEventName = CalendarEntry.ShortDescription


''CurrentEventName = Replace(CurrentEventName,"''","''''")


''替换(CurrentEventName,"''", " \ x27")

替换(CurrentEventName,"''","'")


PopUpWindowTitle = CurrentEventName''现在可以使用


PopupLink =" javascript :PopUpWindow(" &安培; Chr(34)& PopUpWindowTitle&

Chr(34)& , &安培; Chr(34)& CurrentEventDetails& Chr(34)& , &安培; Chr(34)&

CurrentEventContact& Chr(34)& , &安培; Chr(34)& CurrentEventContactEmail&

Chr(34)& , &安培; Chr(34)& CurrentDate& Chr(34)& ")"


strTemp + ="< BR>< A HREF =''#''onClick =''" &安培; PopupLink& ">" &

CurrentEventName& "< / A>< BR>"

结束如果

下一页


返回strTemp

-------------------------

PopupLink的运行时值示例:

javascript :PopUpWindow(鲍勃'的生日派对,鲍勃再过一年!,

鲍勃, Bo *@asdf.com",11/3/2003)"


strTemp的运行时值示例:

"< BR> ;< A HREF =''#''onClick =''javascript :PopUpWindow(Bob'的生日派对,

" Bob让它成为另一个year!"," Bob"," Bo *@asdf.com"," 11/3/2003")''> Bob's>
生日派对< / A> ;< BR>"


价值Bob'的生日派对来自数据库。如果我删除数据库中的

撇号,则不会发生错误。


最后,这显然是一个函数调用; strTemp最终由

写入了Label控件的.text属性的调用代码。


感谢您抽出宝贵的时间......


杰夫

" mikeb" <毫安************ @ mailnull.com>在消息中写道

news:e1 ****************** @ tk2msftngp13.phx.gbl ...
Apparently you saw right through my little ploy to post simpler code to the
newsgroup than what I was actually working with (and with the omission/typo
you correctly concluded). Your <soapbox> points are well taken...

I proceeded to go with your suggestions, but I still get the error. So, I
posted the complete section of code below.

Additionally, on closer observation, the sequence that leads to the error
message appearing is as follows:
1. From the VS.NET IDE, I press F5 to run the project (with the ASPX page in
question set as the start page).
2. The browser window appear, and I see the top part of the ASPX page
rendered (e.g., the page header graphics show up).
3. The messagebox appears displaying the "Error: Unterminated string
constant" message.
4. At this point, only the top part of the page is rendered, and the message
box is displayed in the middle of the browser window. If I click on either
Yes or No (the only two buttons on the message box - prompting to debug or
not), the page does finish rendering in the browser. The page looks fine,
but the links are dead (presumably because the JavaScript function call part
of the link is invalid).

Here''s the code (followed by runtime sample values):

For Each CalendarEntry In m_CalendarEntries
If CalendarEntry.EventDateTime = DateToGet Then

''Get the for the JS popup window ready
CurrentDate = CStr(CalendarEntry.EventDateTime)
CurrentEventDetails = CalendarEntry.LongDescription
CurrentEventContact = CalendarEntry.ContactName
CurrentEventContactEmail = CalendarEntry.ContactEMail
CurrentEventName = CalendarEntry.ShortDescription

''CurrentEventName = Replace(CurrentEventName, "''", "''''")

''Replace(CurrentEventName, "''", "\x27")
Replace(CurrentEventName, "''", "'")

PopUpWindowTitle = CurrentEventName ''will do for now

PopupLink = "javascript:PopUpWindow(" & Chr(34) & PopUpWindowTitle &
Chr(34) & ", " & Chr(34) & CurrentEventDetails & Chr(34) & ", " & Chr(34) &
CurrentEventContact & Chr(34) & ", " & Chr(34) & CurrentEventContactEmail &
Chr(34) & ", " & Chr(34) & CurrentDate & Chr(34) & ")"

strTemp += "<BR><A HREF=''#'' onClick=''" & PopupLink & "''>" &
CurrentEventName & "</A><BR>"
End If
Next

Return strTemp
-------------------------
Sample runtime value of PopupLink:
"javascript:PopUpWindow("Bob''s Birthday Party", "Bob made it another year!",
"Bob", "Bo*@asdf.com", "11/3/2003")"

Sample runtime value of strTemp:
"<BR><A HREF=''#'' onClick=''javascript:PopUpWindow("Bob''s Birthday Party",
"Bob made it another year!", "Bob", "Bo*@asdf.com", "11/3/2003")''>Bob''s
Birthday Party</A><BR>"

The value "Bob''s Birthday Party" comes from a database. If I remove the
apostrophe in the database, then the error does not occur.

Finally, This is obviously a function call; strTemp is eventually written by
the calling code to the .text property of a Label control.

Thanks for taking the time...

Jeff
"mikeb" <ma************@mailnull.com> wrote in message
news:e1******************@tk2msftngp13.phx.gbl...
Jeff S写道:
在一个VB.NET代码隐藏模块中,我为一个将
指向JavaScript函数的链接构建了一个字符串。下面的两行代码显示了相关内容。

PopupLink =" javascript :PopUpWindow(& Chr(34)& PopUpWindowTitle&
Chr(34)&","& Chr(34)& CurrentEventDetails&")"
strTemp + ="< BR>< A HREF =''#' 'onClick =''" &安培; PopupLink& ">" &安培; EventName&
"< / A>< BR>"

我遇到的问题是当字符串变量[PopUpWindowTitle]
或[CurrentEventDetails]包含时带有撇号的字符串,然后
服务器抛出错误未终止的字符串常量。这个错误在浏览器中的页面呈现之前显示
(或者当页面呈现为
浏览器时 - 我不确定是哪一个 - 但它是在页面显示之前在
浏览器中。

我尝试包含JavaScript的\和转义字符 - 但这并不能解决问题(例如,PopUpWindowTitle = Replace(PopUpWindowTitle,
"''"," \''")。服务器仍然会抛出未终止状态字符串常量错误。

我该怎么办?我需要在
JavaScript函数调用字符串中包含撇号,而VB.NET忽略了JavaScript的Escape
字符-yet表示尊重JavaScript的语法规则。

提前致谢。
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant.

PopupLink = "javascript:PopUpWindow(" & Chr(34) & PopUpWindowTitle & Chr(34) & ", " & Chr(34) & CurrentEventDetails & ")"
strTemp += "<BR><A HREF=''#'' onClick=''" & PopupLink & "''>" & EventName &
"</A><BR>"

The problem I have is that when the string variables [PopUpWindowTitle] or [CurrentEventDetails] contain a string with an apostrophe, then the server throws an error "Unterminated String Constant". This error shows up prior to the page rendering in the browser (or as the page is being rendered to the browser - I''m not sure which - but it''s before the page shows up in the
browser).

I tried including JavaScript''s "\" escape character - but this does not
solve the problem (e.g., PopUpWindowTitle= Replace(PopUpWindowTitle, "''", "\'' "). The server still throws the Unterminated String Constant error.

What can I do about this? I need to include apostrophes in the JavaScript function call string and VB.NET is ignoring JavaScript''s Escape
character -yet honoring JavaScript''s syntax rules.

Thanks in advance.



您是否真的在服务器上收到错误,而不是客户?

我可以看到几个可能的问题:

1)在上面的2行样本中,你错过了一个Chr(34)术语
构建PopupLink的表达式。似乎那个
表达式的结尾应该是:

CurrentEventDetails& Chr(34)& )"

不是:

CurrentEventDetails& )

因为你可能需要为客户端PopUpWindow()函数提供
CurrentEventDetails参数的结束双引号。

我会愿意打赌缺少的Chr(34)表达式在你的代码中,它可能只是你帖子中的拼写错误 - < soapbox>我说,
cut-n-paste是你的朋友。在代码
片段中输入拼写错误是不可原谅的,这些片段可以从真实代码中粘贴。我的意思是,如果你的片段中有一个错误的拼写错误,那么读者应该知道问题究竟是什么? < / soapbox>

2)这是我认为是你真正的问题。而不是:

替换(PopUpWindowTitle,"''"," \''")

尝试使用:

替换(PopUpWindowTitle,"''"," \ x27")

或:

替换(PopUpWindowTitle,"''","' ")

浏览器不会对转义序列执行JavaScript样式处理 -
它使用字符实体来转义特殊字符。
浏览器显然需要处理页面,然后才能将页面的一些内容传递给JavaScript处理器。

如果用\ xx27替换撇号。 ,那个字符串对浏览器来说意味着什么,所以这就是传递给JavaScript处理器的处理器,它将它视为撇号的转义序列。

如果用'''替换,则该字符串是一个''Character
Entity'',浏览器在将字符串传递给JavaScript处理器之前将其转换为撇号。 br />
很抱歉这篇长篇文章。
-
mikeb



Are you really getting the error on the server, not the client?

I can see a couple possible problems:

1) in your 2 line sample above, you are missing a Chr(34) term in the
expression that builds PopupLink. it seems that the end of that
expression should be:

CurrentEventDetails & Chr(34) & ")"

not:

CurrentEventDetails & ")"

since you presumably need a closing double quote for the
CurrentEventDetails parameter to the client side PopUpWindow() function.

I''d be willing to bet that the missing Chr(34) expression is in your
code, it''s probably just a typo in your post - <soapbox> to which I say,
"cut-n-paste is your friend". It''s inexcusable to have typos in code
snippets that can be pasted from the real code. I mean, if you have a
typo in your snippet that is supposed to demonstrate the problem, how
are the readers supposed to know what the problem really is? </soapbox>

2) this is what I believe to be your real problem. Instead of:

Replace(PopUpWindowTitle, "''","\'' ")

try using:

Replace(PopUpWindowTitle, "''","\x27")

or:

Replace(PopUpWindowTitle, "''","'")
The browser doesn''t do JavaScript-style handling of escape sequences -
it uses Character Entities to escape special characters. And the
browser obviously needs to process the page before it can pass bits of
the page off to the JavaScript processor.

If you replace apostrophes with "\x27", that string of characters means
nothing to the browser, so that''s what gets passed in to the JavaScript
processor, which sees it as an escape sequence for the apostrophe.

If you replace with "'", that string of characters is an ''Character
Entity'' which the browser translates into an apostrophe before passing
the string on to the JavaScript processor.

Sorry for the long post.
--
mikeb



这篇关于包含JavaScript转义字符的字符串的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆