jquery - Escape apostrophe when passing parameter in onclick event -


I am passing company name for an onclick event. I have added oposcripts in some company names. Change '(' ',' '') 'to company_name field. This allows the onclick event to fire, but the confirmation message is displayed as "Jane and Welding Company"

  ,' & lt;% = Html.Encode (Item.company1.company_name.Replace ("", "& amp; # 39;")%>; ';); "class =" FG-button FG-button-icon-solo ui-state-default- UI-corner-all "& gt; & Lt; Span class = "ui-icon ui-icon-refresh" & gt; & Lt; / Span & gt; & Lt; / A & gt; & Lt; Script type = "text / javascript" & gt; Function ActionClick (URL, Company name) {If (Confirm (This action will activate the company's primary company ('+ Company name +)' and all its other subsidiaries. ') Continue')) { Location.href = url; }; };  

Edit confirmation message & amp; # 39; Instead of the message 'When I type it here, it & amp; # 39; with '. It does not have added spaces I want to know the best way to pass it on my onclic event and display it properly in a variety of messages (if there is a better way).

There are two options as I see it.

  1. If you use the apostrophes / 'single quote (') instead of the parameter (") in the parameter, then you should not have to avoid it at all. HTML encoding is a quote (if they are string However, as javascript is already wrapped in quotes, you will need to avoid your quotes, eg:

    Onclick = "returnclick (\" & lt;% = Url.Action ("active "New" {id = Item.company_id})%>; \ ", \" <% = Html.Encode (item.company1.company_name)%> \ ");" < / P>

  2. Backslash escapes the name of the company because it is the only last JavaScript string, which requires that the apostrophe is saved, not HTML, such as:

    onclick = "return Actionclick ('Lt;% = Url.Action (" activate ", new {id = item.company_id})%>', '& lt;% = Html.Encode (item. Company1.company_name.Replace ("'", "\\' '))% & Gt;');"


Comments