JavaScriptEncode() public static method

javascript 문을 동적으로 생성할 때 script 코드 내용을 javascript 문법에 맞게끔 인코딩한다.
public static JavaScriptEncode ( this value, bool flagForQuote = true ) : string
value this 인코딩할 문자열
flagForQuote bool 인용문구를 씌울 것인가?
return string
Ejemplo n.º 1
0
        /// <summary>
        /// 메시지 출력
        /// page 가 null 이 아니면 RegisterStartupScript 로 스크립트 추가되어지므로 호출후 Response.End()등으로 실행중지시 출력되어지지 않습니다.
        /// </summary>
        /// <param name="messageBoxDisplayKind">출력타입</param>
        /// <param name="title">메시지 캡션</param>
        /// <param name="text">메시지</param>
        /// <param name="messageType">메시지타입</param>
        /// <param name="messageButton">버튼타입</param>
        /// <param name="page">렌더링되는 페이지</param>
        /// <param name="returnUrl">메시지출력후 이동할 url</param>
        /// <param name="endResponse">프로세스 종료여부</param>
        public static void MessageBox(MessageBoxDisplayKind messageBoxDisplayKind,
                                      string title,
                                      string text,
                                      MessageType messageType      = MessageType.Normal,
                                      MessageButtons messageButton = MessageButtons.Ok,
                                      System.Web.UI.Page page      = null,
                                      string returnUrl             = "",
                                      bool endResponse             = true)
        {
            switch (messageBoxDisplayKind)
            {
            case MessageBoxDisplayKind.Page:

                var httpResponse = HttpContext.Current.Response;
                var param        = string.Format("Title={0}&Content={1}&MessageType={2}&MessageButton={3}&ReturnUrl={4}",
                                                 title.UrlEncode(), text.UrlEncode(), (int)messageType, (int)messageButton, returnUrl.UrlEncode());
                var url = UrlParamConcat(AppSettings.MessageBoxUrl, param);
                httpResponse.Redirect(url, endResponse);
                return;

            default:

                #region

                StringBuilder buffer = new StringBuilder();
                try
                {
                    buffer.Append(@"alert(");
                    buffer.Append(AntiXssTool.JavaScriptEncode((title.IsNotWhiteSpace() ? "[" + title + "]\n\n" : string.Empty) + text));
                    buffer.Append(@");");

                    // Alert이고 닫기 타입이 Close 이면 창을 닫는다.
                    if (messageButton == MessageButtons.Close)
                    {
                        buffer.Append(SR.CloseWindowJavascript);
                    }
                    else if (returnUrl.IsNotWhiteSpace())
                    {
                        buffer.AppendFormat("window.location.href={0};", AntiXssTool.JavaScriptEncode(returnUrl));
                    }

                    if (page != null)
                    {
                        //이미 추가되었다면
                        if (page.ClientScript.IsStartupScriptRegistered(page.GetType(), page + "_MessageBox"))
                        {
                            ScriptManager.RegisterStartupScript(page, page.GetType(), page + "_MessageBox", buffer.ToString(), true);
                        }
                        else
                        {
                            ScriptManager.RegisterStartupScript(page, page.GetType(), page + "_MessageBox", buffer.ToString(), true);
                        }
                    }
                    else
                    {
                        HttpContext.Current.Response.Output.Write(WrapScriptTag(buffer.ToString()));
                    }
                }
                finally
                {
                    buffer = null;
                }

                #endregion

                break;
            }
        }
Ejemplo n.º 2
0
        public void EncodeJavascript()
        {
            var encoded = AntiXssTool.JavaScriptEncode("javascript:alert('abc');");

            Console.WriteLine(encoded);
        }