Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Is Ajax right for me?

    What I have works (kind of) but I am looking for a better way. What I have is an FTP script that runs every few minutes and gathers small .txt files from 5 different locations. I then use this to present that information.

    Code:
    Const adOpenStatic = 3
    Const adLockOptimistic = 3
    Const adCmdText = &H0001
    
    Set objConnection = CreateObject("ADODB.Connection")
    Set objRecordSet = CreateObject("ADODB.Recordset")
    
    strPathtoTextFile = "C:\Inetpub\wwwroot\Default\new_web\usbobs\"
    
    objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
              "Data Source=" & strPathtoTextFile & ";" & _
              "Extended Properties=""text;HDR=No;FMT=Delimited"""
    
    strFile = "cf.txt"
    
    objRecordset.Open "SELECT * FROM cf.txt",_
              objConnection, adOpenStatic, adLockOptimistic, adCmdText
    
    count = 4
    do until count = 0
    
    
    
    'Do Until objRecordset.EOF
           
      
     If objRecordset.Fields.Item("BobNumber") = "East Plytrim" Then Response.Write "<span style=""color: rgb(51,0,102);"">Bin: East Plytrim</span>" & "<br>"
     If objRecordset.Fields.Item("BobNumber") = "Center Plytrim" Then Response.Write "<span style=""color: rgb(51,0,102);"">Bin: Center Plytrim</span>" & "<br>"
     If objRecordset.Fields.Item("BobNumber") = "Saw Chips" Then Response.Write "<span style=""color: rgb(51,0,102);"">Bin: Saw Chips</span>" & "<br>"
     If objRecordset.Fields.Item("BobNumber") = "Plychips" Then Response.Write "<span style=""color: rgb(51,0,102);"">Bin: Plychips</span>" & "<br>"
    	If objRecordset.Fields.Item("BinMeasure") >= 17 Then Response.Write "<span style=""color: red; font-weight:bold;"">High Level</span>" & "<br>"
        Response.Write "<span style=""color: rgb(0, 255, 255); font-weight:bold;"">Height: </span>" & "<span style=""color: rgb(0, 255, 255); font-weight:bold;"">" & objRecordset.Fields.Item ("percentage") & "</span>" & "<br>"
        If objRecordset.Fields.Item("Status") = "DISABLED" Then Response.Write "<span style=""color: red; font-weight:bold;"">Status: DISABLED</span>" & "<br>"
        If objRecordset.Fields.Item("Status") = "ENABLED" Then Response.Write "Status: ENABLED" & "<br>" 
        Response.Write "Status: " & objRecordset.Fields.Item("DateTime") & "<br>"
        Response.Write "<br>"
    
    
    objRecordset.MoveNext
    
    count = count - 1
     
    Loop
    
    objRecordset.close
    I also use <meta http-equiv="refresh" content="300"> to refresh the browser every 5 min, but I'm sure this is not the best way to accomplish this. Would Ajax be the right way to go and if so could someone give me a shove in the right direction?

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    AJaX would be slightly better primarily because you can run it in the background and present the information without refreshing the page. Just make the call with AJaX and set an elements innerHTML to the returned value.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #3
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks WolfShade, this gives me a direction to head.

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,301
    Thanks
    10
    Thanked 586 Times in 567 Posts
    since you are spitting out HTML, this is a good candidate for my off-page content fetcher.
    you would need to wrap all those spans in a container, say <div id=main>, and then you can easily inject that content into any page, and do so at a regular interval.


    let's call your data page data.asp and your home page index.html (shown below, adjust as needed):

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    	<title>my home page</title>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    
    <body onload=redraw()>
      
      <h1>my home page</h1>
      
    <h3>Latest Updates</h3>
    <div id='container'>
    	
    </div>	
    
    
    <script type='text/javascript'>
    
    
    function redraw(){
        getPage("data.asp", "#main", "#container");
    }
    
    
     setInterval(redraw, 1000 * 300); // repeat every 300seconds
    
    
    function getPage(url, from, to) {
    	var cached=0 // sessionStorage[url];
       	if(!from){from="body";} // default to grabbing body tag
    	if(cached){return elm.innerHTML=cached;} // cache responses for instant re-use re-use
    	if(to && to.split){to=document.querySelector(to);} // a string TO turns into an element
    	if(!to){to=document.querySelector(from);} // default re-using the source elm as the target elm
    
    	var XHRt = new XMLHttpRequest; // new ajax
    	XHRt.responseType='document';  // ajax2 context and onload() event
    	XHRt.onload= function() { sessionStorage[url]=to.innerHTML= XHRt.response.querySelector(from).innerHTML;};
    	XHRt.open("GET", url, true);
    	XHRt.send();
    	return XHRt;
    }
    
    
    
    </script>
    </body>
    </html>
    EDIT i turned off caching since this is for data, changes in red.
    Last edited by rnd me; 01-30-2013 at 04:54 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

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