Bob Schneider
asked on
JS Issue with apostrophe
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.:
[code]
var obj0;
var obj1;
var obj2;
var j;
var temp;
var i=0;
var speed=25;
var message='<%=sMarqueeMsg%>' ;
function imgScroll() {
obj0.style.left=i+'px';
obj1.style.left=j+'px';
i--;
j--;
if(i<-temp-1) {
i=temp;
}
if(j<-temp-1) {
j=temp;
}
scroller=setTimeout('imgSc roll()',sp eed);
}
window.onload=function() {
obj0=document.getElementBy Id('ticker 0');
obj1=document.getElementBy Id('ticker 1');
obj2=document.getElementBy Id('contai ner');
obj0.firstChild.nodeValue= obj1.first Child.node Value=mess age;
j=obj0.offsetWidth;
temp=j;
obj2.className='setup';
obj0.style.width=obj1.styl e.width=te mp+'px';
imgScroll();
}
[/code]
Here is the asp for the variable:
[code]
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, "''", "'")
rs.Close
Set rs = Nothing
[/code]
Any help would be much appreciated!
[code]
var obj0;
var obj1;
var obj2;
var j;
var temp;
var i=0;
var speed=25;
var message='<%=sMarqueeMsg%>'
function imgScroll() {
obj0.style.left=i+'px';
obj1.style.left=j+'px';
i--;
j--;
if(i<-temp-1) {
i=temp;
}
if(j<-temp-1) {
j=temp;
}
scroller=setTimeout('imgSc
}
window.onload=function() {
obj0=document.getElementBy
obj1=document.getElementBy
obj2=document.getElementBy
obj0.firstChild.nodeValue=
j=obj0.offsetWidth;
temp=j;
obj2.className='setup';
obj0.style.width=obj1.styl
imgScroll();
}
[/code]
Here is the asp for the variable:
[code]
Set rs = Server.CreateObject("ADODB
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, "''", "'")
rs.Close
Set rs = Nothing
[/code]
Any help would be much appreciated!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
becomes:
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,Marqu eeMsg) 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.
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
becomes:
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,Marqu
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.
ASKER
Thank you very much!!! Very informative.
ASKER
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?