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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

use Javascript in VBScript(ASP) routine 1

Status
Not open for further replies.

PaulBricker

Programmer
Sep 25, 2002
3,554
US
What I currently have is a page (sorry it's an intranet site so I can't display it)that has a scroller I got at Dynamic Drive. I have to go in manually and change the content of that scroller each week. I would like to be able to pull the content for the scroller from a database table instead so that the Admin Assistant can make the changes in the table and keep me out of the loop. So to that end, I have this on my page.
Code:
[green]<%ON ERROR RESUME NEXT
IF rst.EOF THEN
	Response.Write "There are no entries in the database."
ELSE        
        Response.Write "var pausecontent2=new Array();"%>[/green]
	<p>
	 [green]<%  i = 0
	DO WHILE NOT rst.EOF 
	 Response.Write "pausecontent2[" & i & "]='" & rst("Dept ID") & "';"[/green]

	<p>
	[green]<%
        i=i+1
		rst.MoveNext
		
	Loop 
	

END IF %>[/green]

The result should produce script that looks like this

Code:
<script type="text/javascript">

var pausecontent2=new Array()
pausecontent2[0]='Dining Hall Windows'
pausecontent2[1]='WB Bathroom conversion'
pausecontent2[2]='Chapel/Parking Lot Light'
pausecontent2[3]='Ath Ctr Rooftop Unit Repair'
pausecontent2[4]='Ath Ctr Wrestling Room Roof'
 
</script>

that get passed to this function

Code:
<script type="text/javascript">

new pausescroller(pausecontent2, "pscroller2", "someclass", 3000)

</script>
The first code does produce a string result that gives me the appearance I need in the array, but, of course, Response.Write just writes it to the page. I need to create a <script type = "text/javascript"></script> array on the page that the function can read.
Clear as mud?
Anyone have any ideas?
I thought I had found a winner here
but I can't get it to work.

Thanks

Paul
 
You need to response.write out the <script> tags as well. It's hard to say what you need to change because you did not provide your actual code. What you posted above will not run - you have 2 <% w/o any closing %>, and you have a couple of <p> tags stuck in server side that don't belong.

Try this:
Code:
<%
   ON ERROR RESUME NEXT
   IF rst.EOF THEN
      Response.Write "There are no entries in the database."
   ELSE        
      %>
         <script type="text/javascript">
            var pausecontent2 = new Array();
            <%
               i = 0
               DO WHILE NOT rst.EOF
                  %>
                     pausecontent2[<%=i%>] = '<%=rst("Dept ID")%>';
                  <%
                  i = i+1
                  rst.MoveNext
               Loop
            %>
         </script>
      <%
   END IF
%>

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
[tt][blue]<script type="text/javascript">[/blue]
<%ON ERROR RESUME NEXT
IF rst.EOF THEN
Response.Write "[red]//[/red]There are no entries in the database." //You don't want to show it on display or do you? You just want to make a comment
ELSE
Response.Write "var pausecontent2=new Array();[blue]\n[/blue]"%>
[red]//<p> //take this out[/red]
<% i = 0
DO WHILE NOT rst.EOF
Response.Write "pausecontent2[" & i & "]='" & rst("Dept ID") & "';[blue]\n[/blue]"

[red]//<p> //take this out[/red]
<%
i=i+1
rst.MoveNext
Loop
END IF
[blue]on error goto 0[/blue]
[blue]'be parsimonious on using on error directive[/blue]
%>
[blue]</script>[/blue]
[/tt]
 
[tt]Response.Write "//There are no entries in the database." [red]'[/red]You don't want to show it on display or do you? You just want to make a comment?[/tt]
 
Sorry, I probably missed a couple things trying to edit the code to be readable. Without the <p> tags the result is just one long string. I could look at adding
<% & vbCrLf & %> in there instead.
I tried the code you posted. It looks like what I got from the 15Seconds site but just gives me a "page can't be found" error. In my editor, I get two script errors

The first one for this line
[green]var pausecontent2 = new Array();[/green]
that just says Syntax Error

The second is for function
[green]new pausescroller(pausecontent2, "pscroller2", "someclass", 3000)[/green]
and says 'pausecontent2 undefined'

I appreciate you looking at this. Any other suggestions or do I need to post anything else? The code for the scroller is quite long so I didn't want to post that. If I take my loop out of page, and just insert
Code:
<script type="text/javascript">

var pausecontent2=new Array()
pausecontent2[0]='Dining Hall Windows'
pausecontent2[1]='WB Bathroom conversion'
pausecontent2[2]='Chapel/Parking Lot Light'
pausecontent2[3]='Ath Ctr Rooftop Unit Repair'
pausecontent2[4]='Ath Ctr Wrestling Room Roof'
 
</script>
The scroller runs just fine so I know the rest of the code works.

Thanks

Paul
 
Yes replace my \n by vbcrlf.
[tt]Response.Write "var pausecontent2=new Array()[red];" & vbcrlf[/red]%>[/tt]
and[tt]
Response.Write "pausecontent2[" & i & "]='" & rst("Dept ID") & "'[red];" & vbcrlf[/red][/tt]
 
tsuji and kaht,
currently, this is what I'm trying.

<script type="text/javascript">
<%
Response.Write "var pausecontent2=new Array(); & vbcrlf "%>

<% i = 0
DO WHILE NOT rst.EOF
Response.Write "pausecontent2[" & i & "]='" & rst("Dept ID") & "'; & vbcrlf
<%
i=i+1
rst.MoveNext
Loop
on error goto 0
'be parsimonious on using on error directive
%>
</script>

<%rst.Close
objConn.Close
Set rst = Nothing
Set objConn = Nothing
%>

<script type="text/javascript">

new pausescroller(pausecontent2, "pscroller2", "someclass", 3000)

</script>

I'm still getting a page can't be found error. My editor throws up a Syntax Error at the first <% inside the script tag and gives me the same error (pausecontent2 undefined) at the line
new pausescroller(pausecontent2,"pscroller2","someclass",3000)

Also, after my second [blue]vbCrLf[/blue], I seem to be missing a closing %> tag. I did insert one after the vbCrLf constant, but it did not make any difference.

Thanks

Paul
 
Try this.
[tt]<script type="text/javascript" defer>[/tt]
for the pausescroller sector.
Worse upon worse, put that function pausescroller inside a window onload event. That is preferrable as far as I'm concerned as that is executed under orderly timeing control.
 
Paul, you should be able to copy/paste my code directly into your page overwriting this:
Code:
<script type="text/javascript">
<%   
    Response.Write "var pausecontent2=new Array(); & vbcrlf "%>
    
     <%  i = 0  
    DO WHILE NOT rst.EOF
     Response.Write "pausecontent2[" & i & "]='" & rst("Dept ID") & "'; & vbcrlf    
    [!]<%[/!]
        i=i+1
        rst.MoveNext
    Loop
on error goto 0
'be parsimonious on using on error directive
%>
</script>
If you insist on using your code, then take out the asp break tag that I have highlighted in red - it's completely unnecessary. <% and %> switch you between server side code and client side code.


-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
kaht - originally, I did copy and paste your code directly into my routine. Here is what I used inside the <body> tags. I did take out the If Then part just to clean it up.
<body>
<%

strQuery ="Select [weeklysch] FROM tblWeeklySch"

Dim objConn, dbPath, strProvider, rst
dbPath = "C:\WEBSITES\physplantdb\PysPlant2002.mdb"
Set objConn = Server.CreateObject("ADODB.Connection")
strProvider = "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" & dbPath
objConn.Open strProvider

Set rst = Server.CreateObject("ADODB.recordset")
rst.Open strQuery, strProvider,3,3%>
[blue]
<%
ON ERROR RESUME NEXT
%>
<script type="text/javascript">
var pausecontent2 = new Array();
<%
i = 0
DO WHILE NOT rst.EOF
%>
pausecontent2[<%=i%>] = '<%=rst("weeklysch")%>';
<%
i = i+1
rst.MoveNext
Loop
%>
</script>[/blue]

<%rst.Close
objConn.Close
Set rst = Nothing
Set objConn = Nothing
%>
<script type="text/javascript">

new pausescroller(pausecontent2, "pscroller2", "someclass", 3000)

</script>



</body>

But it didn't work. It gave me the errors I've described above.

Thanks

Paul

 
tsuji - I copied and pasted your code as well. I'm getting the same editor and page errors with both codes. It does not seem to like the <% that follows the line

<script type = "text/javascript">


because it give me a syntax error at that line.

Thanks

Paul

 
If I change the script tag to
<%@ language = "javascript">

I don't throw the first editor 'syntax error' but I still get the second 'pausecontent2 undefined' error.

Progress??

Paul
 
Just thinking out loud, could I put that entire string into an ASP variable and use that inside the script tags?
Something like

dim strArray
<%
i = 0
DO WHILE NOT rst.EOF
%>
strArray = strArray & "pausecontent2[" & i & "]= '" & rst("weeklysch") & "';" & vbCrLf

<%

i = i+1
rst.MoveNext
Loop
%>

And then below put something like

<script type = "text/javascript">
var pausecontent2=new Array();

<%=strArray%>

</script>

Paul


 
You could, but it wouldn't be any different than kicking it out one line at a time. Paul, can you post your client side code for this problem - what's printed out when you do a view source of the page. The question is teetering on the line between ASP and Javascript. If you post your client side code then we can see what the javascript is doing. If we deem that it looks fine, then you should probably direct all further questions to the ASP forum: forum333

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
OK, here it is. It's the part in bold at the bottom that we are working on.
Thanks

Paul

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<style type = "text/css">
#pscroller2{
width: 250px;
height: 20px;
/*border: 1px solid black;*/
padding: 3px;
}

#pscroller2 a{
text-decoration: none;
}

.someclass{ //class to apply to your scroller(s) if desired
}



</style>

<script type="text/javascript">

/***********************************************
* Pausing up-down scroller- © Dynamic Drive (* This notice MUST stay intact for legal use
* Visit for this script and 100s more.
***********************************************/

function pausescroller(content, divId, divClass, delay){
this.content=content //message array content
this.tickerid=divId //ID of ticker div to display information
this.delay=delay //Delay between msg change, in miliseconds.
this.mouseoverBol=0 //Boolean to indicate whether mouse is currently over scroller (and pause it if it is)
this.hiddendivpointer=1 //index of message array for hidden div
document.write('<div id="'+divId+'" class="'+divClass+'" style="position: relative; overflow: hidden"><div class="innerDiv" style="position: absolute; width: 100%" id="'+divId+'1">'+content[0]+'</div><div class="innerDiv" style="position: absolute; width: 100%; visibility: hidden" id="'+divId+'2">'+content[1]+'</div></div>')
var scrollerinstance=this
if (window.addEventListener) //run onload in DOM2 browsers
window.addEventListener("load", function(){scrollerinstance.initialize()}, false)
else if (window.attachEvent) //run onload in IE5.5+
window.attachEvent("onload", function(){scrollerinstance.initialize()})
else if (document.getElementById) //if legacy DOM browsers, just start scroller after 0.5 sec
setTimeout(function(){scrollerinstance.initialize()}, 500)
}

// -------------------------------------------------------------------
// initialize()- Initialize scroller method.
// -Get div objects, set initial positions, start up down animation
// -------------------------------------------------------------------

pausescroller.prototype.initialize=function(){
this.tickerdiv=document.getElementById(this.tickerid)
this.visiblediv=document.getElementById(this.tickerid+"1")
this.hiddendiv=document.getElementById(this.tickerid+"2")
this.visibledivtop=parseInt(pausescroller.getCSSpadding(this.tickerdiv))
//set width of inner DIVs to outer DIV's width minus padding (padding assumed to be top padding x 2)
this.visiblediv.style.width=this.hiddendiv.style.width=this.tickerdiv.offsetWidth-(this.visibledivtop*2)+"px"
this.getinline(this.visiblediv, this.hiddendiv)
this.hiddendiv.style.visibility="visible"
var scrollerinstance=this
document.getElementById(this.tickerid).onmouseover=function(){scrollerinstance.mouseoverBol=1}
document.getElementById(this.tickerid).onmouseout=function(){scrollerinstance.mouseoverBol=0}
if (window.attachEvent) //Clean up loose references in IE
window.attachEvent("onunload", function(){scrollerinstance.tickerdiv.onmouseover=scrollerinstance.tickerdiv.onmouseout=null})
setTimeout(function(){scrollerinstance.animateup()}, this.delay)
}


// -------------------------------------------------------------------
// animateup()- Move the two inner divs of the scroller up and in sync
// -------------------------------------------------------------------

pausescroller.prototype.animateup=function(){
var scrollerinstance=this
if (parseInt(this.hiddendiv.style.top)>(this.visibledivtop+5)){
this.visiblediv.style.top=parseInt(this.visiblediv.style.top)-5+"px"
this.hiddendiv.style.top=parseInt(this.hiddendiv.style.top)-5+"px"
setTimeout(function(){scrollerinstance.animateup()}, 50)
}
else{
this.getinline(this.hiddendiv, this.visiblediv)
this.swapdivs()
setTimeout(function(){scrollerinstance.setmessage()}, this.delay)
}
}

// -------------------------------------------------------------------
// swapdivs()- Swap between which is the visible and which is the hidden div
// -------------------------------------------------------------------

pausescroller.prototype.swapdivs=function(){
var tempcontainer=this.visiblediv
this.visiblediv=this.hiddendiv
this.hiddendiv=tempcontainer
}

pausescroller.prototype.getinline=function(div1, div2){
div1.style.top=this.visibledivtop+"px"
div2.style.top=Math.max(div1.parentNode.offsetHeight, div1.offsetHeight)+"px"
}

// -------------------------------------------------------------------
// setmessage()- Populate the hidden div with the next message before it's visible
// -------------------------------------------------------------------

pausescroller.prototype.setmessage=function(){
var scrollerinstance=this
if (this.mouseoverBol==1) //if mouse is currently over scoller, do nothing (pause it)
setTimeout(function(){scrollerinstance.setmessage()}, 100)
else{
var i=this.hiddendivpointer
var ceiling=this.content.length
this.hiddendivpointer=(i+1>ceiling-1)? 0 : i+1
this.hiddendiv.innerHTML=this.content[this.hiddendivpointer]
this.animateup()
}
}

pausescroller.getCSSpadding=function(tickerobj){ //get CSS padding value, if any
if (tickerobj.currentStyle)
return tickerobj.currentStyle["paddingTop"]
else if (window.getComputedStyle) //if DOM2
return window.getComputedStyle(tickerobj, "").getPropertyValue("padding-top")
else
return 0
}

</script>
</head>

<body>

<%

strQuery ="Select [Dept ID] FROM Dept"

Dim objConn, dbPath, strProvider, rst
dbPath = "C:\WEBSITES\physplantdb\PysPlant2002.mdb"
Set objConn = Server.CreateObject("ADODB.Connection")
strProvider = "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" & dbPath
objConn.Open strProvider

Set rst = Server.CreateObject("ADODB.recordset")
rst.Open strQuery, strProvider,3,3%><p><%
response.write "the number of records is " & rst.recordcount
%>
<%
ON ERROR RESUME NEXT
%>
<script type="text/javascript">
var pausecontent2 = new Array();
<%
i = 0
DO WHILE NOT rst.EOF
%>
pausecontent2[<%=i%>] = '<%=rst("Dept ID")%>';
<%
i = i+1
rst.MoveNext
Loop
%>
</script>

<%rst.Close
objConn.Close
Set rst = Nothing
Set objConn = Nothing
%>

<script type="text/javascript">

new pausescroller(pausecontent2, "pscroller2", "someclass", 3000)

</script>




</body>
</html>
 
I have made some progress.

kaht, I don't think the stuff I posted above is what you were looking for after looking at what shows when I actually get a page to view. The view I got for you, is from a page that doesn't really display (page can't be found error).
Here is what I did. I passed the string to a variable and then ran the variable in the javascript tags but, I limited the loop to one cycle so that only one record was brought back from the database. It worked. What is happening is that the string being created is all on one line so it's failing when the function looks at it.
When I limited the loop to one record, that problem disappeared. Here is the View...Source for that page.

</script>
</head>

<body>
<p>
pausecontent2[0]='AGND'

<script type = "text/javascript">

var pausecontent2=new Array()
pausecontent2[0]='AGND'


</script>
<script type="text/javascript">

new pausescroller(pausecontent2, "pscroller2", "someclass", 3000)

</script>


Any idea how to add a vbCrLf to the loop? Just using
&vbCrLf
doesn't seem to inject the necessary breaks.
I tried adding & "<p>" but the javascript didn't like that.

Thanks

Paul
 
The problem is that you are not getting a semicolon after your javascript commands. If you have a semicolon after each command then you could put every line of javascript all on one line. For example:
Code:
<script type = "text/javascript">

var pausecontent2=new Array()
pausecontent2[0]='AGND'pausecontent2[1]='AGND'pausecontent2[1]='AGND'


</script>
is not the same as this:
Code:
<script type = "text/javascript">

var pausecontent2=new Array()[!];[/!]
pausecontent2[0]='AGND'[!];[/!]pausecontent2[1]='AGND'[!];[/!]pausecontent2[1]='AGND'[!];[/!]


</script>

I find it odd that you're getting no semicolons in your view-source when it exists in the server side code:
Code:
                  %>
                     pausecontent2[<%=i%>] = '<%=rst("Dept ID")%>'; [!]<-- semicolon right there[/!]
                  <%

Not to mention, with the last method posted directly above, the %> and <% are on completely seperate lines as the javascript declaration that is being served to the browser, which means that there would be a carriage return between every command - meaning they wouldn't all be on the same line anyway.

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
OK. I'm home right now without the file, so I'll have to test things in the morning. I must confess that I didn't realize you could debug the code using the View..Source of the page (still learning). That made a huge difference for me when I could actually see what the browser was doing.
I really appreciate your help. The javascript is still way above my head but I'm looking forward to the climb. I'll try tearing it apart a little at a time and see what I come up with.

Paul
 
Good luck [thumbsup2]

On a side note, you should probably start a new thread for further questions, most of the time people (myself included) stay away from the 10+ post threads.

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
kaht, just a last note to say I got it working both ways. I'm not sure what was hanging it up, but I started with a fresh page, and your code worked just as written.

Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top