SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how do I get dynamic arrays as values for the javascript?

    I know very little javascript and need to use it to pt a banner across a page, where it is pulling from values a customer puts in the backend.Here is the javascript:
    Code:
    <style type="text/css">
    
    #memoryticker{
    background-color: lightyellow;
    width: 800px;
    font: bold 12px Verdana;
    /*Tip: add in height attribute here for multiple line scroller*/
    border: 1px solid black;
    padding: 3px;
    position: absolute; top: 208px; right: 100px; 
    /*Change 0.7 below to a different number if desired (0.7=0.7 seconds)*/
    /*Remove below line to remove transitional effect in IE. Below line should always appear last within this CSS*/
    filter: progid:DXImageTransform.Microsoft.GradientWipe(GradientSize=1.0 Duration=0.7)
    }
    
    </style>
    <script type="text/javascript">
    var tickercontents=new Array()
    
    tickercontents[0]='#thedesc#'
    tickercontents[1]='2) STPPOA Monthly Board Meeting Minutes are now posted on the web. <a href="http://www.seatrailplantationpoa.org/POA/POA_Minutes.htm"> Click here to view now. </a>'
    tickercontents[2]='3) Brunswick County needs your help.  <a href="http://www.seatrailplantationpoa.org/bc_survey.htm"> Please click here and take attached survey! </a>'
    
    var persistlastviewedmsg=1 //should messages' order persist after users navigate away (1=yes, 0=no)?
    var persistmsgbehavior="onload" //set to "onload" or "onclick".
    
    //configure the below variable to determine the delay between ticking of messages (in miliseconds):
    var tickdelay=6000
    
    ////Do not edit pass this line////////////////
    ' <a href="http://www.freewarejava.com">FreewareJava</a>- Free Java applets, tutorials, and resources.</a>'
    
    var divonclick=(persistlastviewedmsg && persistmsgbehavior=="onclick")? 'onClick="savelastmsg()" ' : ''
    var currentmessage=0
    
    function changetickercontent(){
    if (crosstick.filters && crosstick.filters.length>0)
    crosstick.filters[0].Apply()
    crosstick.innerHTML=tickercontents[currentmessage]
    if (crosstick.filters && crosstick.filters.length>0)
    crosstick.filters[0].Play()
    currentmessage=(currentmessage==tickercontents.length-1)? currentmessage=0 : currentmessage+1
    var filterduration=(crosstick.filters&&crosstick.filters.length>0)? crosstick.filters[0].duration*1000 : 0
    setTimeout("changetickercontent()",tickdelay+filterduration)
    }
    
    function beginticker(){
    if (persistlastviewedmsg && get_cookie("lastmsgnum")!="")
    revivelastmsg()
    crosstick=document.getElementById? document.getElementById("memoryticker") : document.all.memoryticker
    changetickercontent()
    }
    
    function get_cookie(Name) {
    var search = Name + "="
    var returnvalue = ""
    if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    if (offset != -1) {
    offset += search.length
    end = document.cookie.indexOf(";", offset)
    if (end == -1)
    end = document.cookie.length;
    returnvalue=unescape(document.cookie.substring(offset, end))
    }
    }
    return returnvalue;
    }
    
    function savelastmsg(){
    document.cookie="lastmsgnum="+currentmessage
    }
    
    function revivelastmsg(){
    currentmessage=parseInt(get_cookie("lastmsgnum"))
    currentmessage=(currentmessage==0)? tickercontents.length-1 : currentmessage-1
    }
    
    if (persistlastviewedmsg && persistmsgbehavior=="onload")
    window.onunload=savelastmsg
    
    if (document.all||document.getElementById)
    document.write('<div id="memoryticker" '+divonclick+'></div>')
    if (window.addEventListener)
    window.addEventListener("load", beginticker, false)
    else if (window.attachEvent)
    window.attachEvent("onload", beginticker)
    else if (document.all || document.getElementById)
    window.onload=beginticker
    
    </script>
    how do i get the array values to be a variable kept track of with an iD?

  2. #2
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please be more specific. What are you trying to accomplish with the array? What do you mean by 'id' ?

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the text pulls from a backend of a website, which i wrote in coldfusion. Each line of text is put in the database with an id. The id is used in the coldfusion pages to add/edit/or delete the lines of text. Need to know if it is possible to put the lines of text into the arrray[0]-[2] in the javascript, and if so how this is done?

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    easy way:
    use coldfusion to write out javascript. I know buggerall about coldfusion, but if it were ASP it'd be something like
    Code:
    <script type="text/javascript">
    var myArray= [
    "<&#37;=thing1%>",
    "<%=thing2%>",
    "<%=thing3%>"
    ];
    </script>
    harder way:
    use ajax to get a list of lines of text

  5. #5
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by shaggysc96 View Post
    the text pulls from a backend of a website, which i wrote in coldfusion. Each line of text is put in the database with an id. The id is used in the coldfusion pages to add/edit/or delete the lines of text. Need to know if it is possible to put the lines of text into the arrray[0]-[2] in the javascript, and if so how this is done?
    There's a couple of options here. One of the issues you *may* run into is where you're attempting to store the ID of the table row as the index of the array. It works find as long as every ID in the database is in numerical order and you're willing to check your array for empty values in a loop. For example:
    Code:
    database:
    id = 1, value = "something"
    id = 2, value = "something2"
    id = 4, value = "something4"
    You can see that the gap between 2 and 4 could cause problems if you tried to use those ID's as array indexes in javascript.

    To overcome this, I've used an "array of objects" in javascript to give some meaning to the data and not have to worry about the indices malfunctioning:
    Code:
    var jsArray = [
        {id:1,value:"something"},
        {id:2,value:"something2"},
        {id:4,value:"something4"}
    ];
    for (var i = 0; i < jsArray.length; i++) {
      alert(jsArray[i].id + " = " + jsArray[i].value);
    }
    Hope this helps,

    Jon


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •