Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple syntax for asp code in between javascript script tags 1

Status
Not open for further replies.

shopwise

Technical User
Sep 22, 2008
52
US
I need to place the following asp code in between javascript script tags but don't know the proper syntax so that the javascript will render the asp code properly (and if any symbols need to be escaped):
Code:
<% 
dim no
no=0
Response.Write "fcontent[" & no & "]=Recordset1("Company") -  Recordset1("City"), Recordset1("State")
%>

So that it renders in the html to something like this:
Code:
fcontent[1]=Knox Church - Toronto, ON
 
You need to put quotes (single or double) around your JS string literals and then escape any of those quotes inside your string.

You should ask in the ASP / VB forums for the syntax to escape these quotes programatically, as it is not a JS question.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
If you want to render it as some innerHTML such as <div>...<div>, it is this.
[tt]
Response.Write "fcontent[" & [red]([/red]no[red]+1)[/red] & "]=[red]" &[/red] Recordset1("Company")[red].value & "[/red] - [red]" &[/red] Recordset1("City")[red].value & "[/red],[red]" &[/red] Recordset1("State")[red].value[/red]
[/tt]
That should show a line as asked.

But if you in fact output it to script section as a variable fcontent[1], then you should put another pair of quote on the right hand side.
[tt]
Response.Write "fcontent[" & [red]([/red]no[red]+1)[/red] & "]=[red]""" &[/red] Recordset1("Company")[red].value & "[/red] - [red]" &[/red] Recordset1("City")[red].value & "[/red],[red]" &[/red] Recordset1("State")[red].value[/red] [red]& """"[/red]
[/tt]
 
I did not think that it was necessary to provide more code but maybe it is. I've tried both code versions provided by tsuji but none of the code enclosed by the asp tags are rendered in the html. It is just blank.
Additional code follows
Code:
<script type="text/javascript">

/***********************************************
* Fading Scroller- © Dynamic Drive DHTML code library ([URL unfurl="true"]www.dynamicdrive.com)[/URL]
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at [URL unfurl="true"]http://www.dynamicdrive.com/[/URL] for full source code
***********************************************/

var delay = 2000; //set delay between message change (in miliseconds)
var maxsteps=30; // number of steps to take to change from start color to endcolor
var stepdelay=40; // time in miliseconds of a single step
//**Note: maxsteps*stepdelay will be total time in miliseconds of fading effect
var startcolor= new Array(255,255,255); // start color (red, green, blue)
var endcolor=new Array(0,0,0); // end color (red, green, blue)

var fcontent=new Array();
begintag='<div style="font: normal 14px Arial; padding: 5px;">'; //set opening tag, such as font declarations

 <%
 Dim no
 no=0
While ((Repeat1__numRows <> 0) AND (NOT Recordset1.EOF))                                                    
Response.Write "fcontent[" & (no+1) & "]=""" & Recordset1("Company").value & " - " & Recordset1("City").value & "," & Recordset1("State").value & """"
 no = no + 1
  Repeat1__index=Repeat1__index+1
  Repeat1__numRows=Repeat1__numRows-1
  Recordset1.MoveNext()
Wend
%>
closetag='</div>';
 
I do not read the followup script. I don't want to because if what I posted really does not mean anything, you have to pursuit the problem in the asp forum which I've found thereafter you have posted. You should not cross-post or not mentioned it in the first place.
 
I've posted to the asp forum prior to this forum but the participants did not seem to target the javascript rendering issue which is why I resorted to the javascript forum.

I am now working with your supplied code but it is still not rendering in the html.

thanks for your assistance and hopefully with your continued help, the issue will be resolved.
 
If you look at the HTML output of your ASP, you will probably be able to see some errors.

Showing the output rather than the ASP script would get you better responses here, too.

Lee
 
the problem is that the html is blank, meaning that anything contained in the asp tags is entirely stripped.
 
First you need to sort out your ASP so at least it outputs something. That is not problem for this forum.

Once you have your ASP outputting anything to the page, then you need to follow my earlier advice:

You need to put quotes (single or double) around your JS string literals

You should also follow my other advice:

And then escape any of those quotes inside your string. You should ask in the ASP / VB forums for the syntax to escape these quotes programatically

so you don't end up closing your string if you have an apostrophe or double quote in it.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Okay, I have taken a look of your expanded post.

[1] Make sure you have defined some Repeat1__numRow be some positive integer. I suppose it is so somewhere above the script shown, no zero to begin with.

[2] Then I suppose the Recordset1 is not pointing to eof!

[3] As js array is zero-based, (no+1) should better be replaced by (no) so that you won't leave index 0 hanging for no purpose.

[4] You can test out the functionality by leaving out all the unrelated. Only keep the lines to establish the recordset.
[tt]
<html>
<head>
[blue]<%
Repeat1__numRows=100 'arbitrary
[/blue][red]
'here establish the RecordSet1
'etc etc
'strip down your existing lines and retain only the essential for that purpose of setting up RecordSet1[/red]
[blue]%>

<!-- this is basically the part you've shown -->[/blue]
<script type="text/javascript">

/***********************************************
* Fading Scroller- © Dynamic Drive DHTML code library (* This notice MUST stay intact for legal use
* Visit Dynamic Drive at for full source code
***********************************************/

var delay = 2000; //set delay between message change (in miliseconds)
var maxsteps=30; // number of steps to take to change from start color to endcolor
var stepdelay=40; // time in miliseconds of a single step
//**Note: maxsteps*stepdelay will be total time in miliseconds of fading effect
var startcolor= new Array(255,255,255); // start color (red, green, blue)
var endcolor=new Array(0,0,0); // end color (red, green, blue)

var fcontent=new Array();
begintag='<div style="font: normal 14px Arial; padding: 5px;">'; //set opening tag, such as font declarations

<%
Dim no
no=0
While ((Repeat1__numRows <> 0) AND (NOT Recordset1.EOF))
Response.Write "fcontent[" & (no) & "]=""" & Recordset1("Company").value & " - " & Recordset1("City").value & "," & Recordset1("State").value & """"
no = no + 1
Repeat1__index=Repeat1__index+1 'don't know what it is
Repeat1__numRows=Repeat1__numRows-1
Recordset1.MoveNext()
Wend
%>
closetag='</div>';
[blue]
window.onload=function() {
var odiv=document.getElementById("divid");
odiv.innerHTML=fcontent.join("<br />");
}
</script>
</head>
<body>
<div id="divid"></div.
</body>
<html>
[/blue][/tt]
[5] Load the above somename.asp. What do you get?
 
I did give you a fairly complete (asp & javascript)solution in the asp forum if you want to have a look.
thread333-1502071

I don't use the marquee, I use the scroller, but the dynamic building of the content is the same and probably calling the asp page from the script tags is the same.

Paul

PS, I got my solution from these same forums a while back.

 
Thank you all for your valuable input!

After much playing around and testing, I have come to realize that the problem lies in the actual query that the values are being taken from.

The reason I did not detect this as a possible problem is because the identical recordset values display perfectly elsewhere on the page when not placed withing script tags. I suppose the javascript is having a more difficult time translating these recordset values for a reason I haven't yet figured out.

 
can the problem lie in the actual values stored in the database that contain charactors that are tripping the javascript? such as an apostrophe? There are several apostrophes in this query.

If yes, How do I avoid this?
 
Further note
Just for completeness, the looping part needs adding carriage return to make the writng of js variable properly delimited. (Also adding semi-colon does the same; adding both would be clearer.) The corresponding line should be read like this in a loop.
[tt]
Response.Write "fcontent[" & (no) & "]=""" & Recordset1("Company").value & " - " & Recordset1("City").value & "," & Recordset1("State").value & """" [red]& ";" & vbcrlf[/red]
[/tt]
 
can the problem lie in the actual values stored in the database that contain charactors that are tripping the javascript? such as an apostrophe? There are several apostrophes in this query.

If yes, How do I avoid this?

I've told you TWICE already. Are you incapable of understanding this:

And then escape any of those quotes inside your string. You should ask in the ASP / VB forums for the syntax to escape these quotes programatically

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top