JS Issue with apostrophe

Posted on 2009-12-30
Last Modified: 2012-05-08
I am having trouble with a scrolling marquee if the message I want to scroll contains an apostrophe.  Here is the code (note that sMarqueeMsg is an asp variable drawing it's value from a sql server db.:

var obj0;
  var obj1;
  var obj2;
  var j;
  var temp;
  var i=0;
  var speed=25;
  var message='<%=sMarqueeMsg%>';

function imgScroll() {'px';'px';

if(i<-temp-1) {

if(j<-temp-1) {

window.onload=function() {


Here is the asp for the variable:
      Set rs = Server.CreateObject("ADODB.Recordset")
      sql = "SELECT Message FROM MarqueeMsg WHERE SchoolsID = " & Session("school_id")
      rs.Open sql, conn, 1, 2
      If rs.RecordCount> 0 Then sMarqueeMsg = Space(25) & Replace(rs(0).Value, "''", "'")
      Set rs = Nothing

Any help would be much appreciated!
Question by:Bob Schneider
    LVL 40

    Assisted Solution

    replace apostrophe with &#39;

    LVL 3

    Assisted Solution

    On the server side your expression
        Replace(rs(0).Value, "''", "'")
    will replace each instance of two consecutive single quotes in your message by one single quote.  In fact, you want to do the opposite, so that a message such as "bob's problem" becomes "bob''s problem".  Then the code generated from your line
       var message='<%=sMarqueeMsg%>';
    will be
       var message='bob''s problem';
    which will give variable message the value "bob's problem".

    Is that clear?  In brief, change the line in your asp code to read:
       If rs.RecordCount> 0 Then sMarqueeMsg = Space(25) & Replace(rs(0).Value, "'", "''")

    Author Comment

    by:Bob Schneider
    1) I tried the first solution using sMarqueeMsg = Replace(sMarqueeMsg, "'", "&#39") and it rendered &#39 where there should have been an apostrophe in the marquee.

    2) Regarding the suggested backwards escape of the apostrophe, when I send it into the db I replace the single apostrophe with double apostrophe so that the sql works.  Then I replace the double with the single bringing it back out.  This is why the replace is as it is.  Are you saying that is not correct?
    LVL 75

    Accepted Solution

    you want to replace all single quotes by \'

    LVL 3

    Expert Comment

    Sorry, I was getting my string literal syntaxes confused.

    To consider your first bit of code: this is mainly JavaScript to be executed at the browser, but one bit (with <%=sMarqueeMsg%>) will be executed at the server to produce a line of code like:
       var message='Merry Xmas';
    However, if sMarqueeMsg contains the string to be displayed, we have a problem if that string contains anything that might be interpreted as a escape sequence when the JavaScript is run in the browser.  Thus if we want to display
       Happy Xmas's.  \ is a backslash
    we must generate code like
       var message='Happy Xmas\'s.  \\ is a backslash';

    Now, you might think that
       Replace( Replace( sMarqueeMsg, "\", "\\"), "'", "\'" )
    would do the trick, but of course this is VBScript and the literal strings have to follow the escape conventions for that language.  So the final line of code
       var message='<%=Replace( Replace( sMarqueeMsg,
             chr(92), chr(92)&chr(92)), "'", chr(92)&"'" )%>';

    The second thing to consider is whether you need to half the number of quotes in the string retrieved from SQL.  I rather suspect that you don't.  If you put it in the database by constructing and executing an SQL statement along the lines of
       insert into MarqueeMsg(SchoolsId,MarqueeMsg) values (23,"Bob''s problem")
    then the SQL rules for escapes in string literals would have converted to doubled character into a single one.  The code you have:
       If rs.RecordCount> 0 Then sMarqueeMsg = Space(25) & Replace(rs(0).Value, "''", "'")
    would then do nothing unless you actually had two quotes in your original, in which case it would incorrectly remove one of them.  It would be worth testing this case when everything else is working to be sure you have it right.

    Author Comment

    by:Bob Schneider
    Thank you very much!!!  Very informative.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    In my daily work (mainly using, I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
    In Part 1 ( we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
    Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
    Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now