Search code examples
javascriptsharepoint-2007

JS Alert not showing when using Sharepoint list value


I am using Sharepoint 2007 and writing a web part. In the "RenderContents" method I am writing the html code for displaying page. there is a requirement to display alert message to user when they click a link. I have written following code-

string alertmessage = Utility.GetLocalizedText("NavigatingToNewPageTxt", "RCCResources", "Common");

writer.Write("<a href='" + clubMemberReportsLink + "' target='new' onClick='alert('" + alertmessage + "');' > ");

Note- My requirement is to get the alert message from Sharepoint list as we use SP list for translations. when I refreshed the site link was displayed but alert message did not appear.when I checked what was rendered in browser I got following code in browser.

<a href="../../securememberservices/Pages/ContribReport.aspx" target="new" onclick="alert(" this="" is');'=""> ClubLeaderDownloadreportsText</a>

I tried using following code as well

writer.Write("<a href='" + clubMemberReportsLink + "' target='new' onClick=alert('" + alertmessage + "'); > ");

(I removed the single quote from onclick method.)Still the browser does not display alert message. this behavior is observed in both browsers. I know I am missing something very simple here... can you pleas point out any help?


Solution

  • Seems like you need to escape the quotation marks for so that the onclick string is not terminated. Like:

    onclick='alert(\"" + alertmessage + "\");'
    

    Hope that helps!