Javascript only works when Alert present

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • clentoc
    New Member
    • Sep 2007
    • 2

    #1

    Javascript only works when Alert present

    I'm pretty new to javascript so this may well be a really basic error, but I've spent hours trying to fix it with no joy.

    I have written some code which looks in a CSV file, filters it depending on the web page you are looking at and then displays the results as a table. There is an 'IF' statement which only writes the table if there are filtered records to display.

    Now for the bit that is driving me crazy . . . The function only works if there is an 'alert' statement in the code. If this is removed the table doesn't display, when it's there it does ! It's as though the page loads before executing the filter count or IF and the alert gives it that time to do so. If I take out the alert and the IF then it works fine, but I get tables when there is no data and that's what I want to avoid.

    Strangely if you load the page (it doesn't work), navigate to another page and then press the back button to the original page, it does work without the alert being present ?????

    The code is sitting in a JS file and is called from within the body of a web page by : <script>Equival enceTable()</script>

    This is the code :

    [CODE=javascript]function EquivalenceTabl e() {

    // Determines the path of the page
    var sPath = window.location .pathname;

    // Sets up a databound table
    document.write( "<object id='swdata' CLASSID='clsid: 333C7BC4-460F-11D0-BC04-0080C7055A83' >");
    document.write( "<param name='DataURL' value='/showcase1/OPD_Alpha_List/ee_nnw1.csv'>") ;
    document.write( "<param name='UseHeader ' value='True'>") ;
    document.write( "<param name='CaseSensi tive' value='False'>" );
    document.write( "</object>");

    alert('Hello'); // REMOVE THIS AND CODE STOPS WORKING !

    // Filter the table on the path value
    swdata.filter = ""
    swdata.filter = "Source = *" + sPath +"*";
    swdata.sort = "ModelPubNa me" <!-- This sorts the results numerically --!>
    swdata.reset()
    var tableRecordCoun t = swdata.recordse t.recordCount; // Counts the numbr of filtered records


    // Draws a table of filtered data if there are records to display
    if (tableRecordCou nt > 0 )
    {
    document.write( "</p>");
    document.write( "<table border='0' width='50%' summary='Table of equivalent OPDs'cellspacin g='1' bgcolor='#00000 0' id='swtable' datasrc='#swdat a' cellpadding='3' datapagesize='2 5' id='datatable'> ");
    document.write( "<thead>");
    document.write( "<th bgcolor='#CC330 0' width='50%'><p align='center'> <a><font color='black'>< strong>Equivale nt OPD(s)</strong></font></a></p></th>");
    document.write( "<th bgcolor='#F5CB9 9' width='70%'><p align='center'> <a><font color='black'>< strong>Processi ng Area</strong></font></a></p></th>");

    document.write( "</thead>");
    document.write( "<tbody>");
    document.write( "<tr bgcolor='#F5CB9 9' valign='top'>") ;
    document.write( "<td valign='top' bgcolor='#FFFFF F' align='left'><a datafld='Hyperl ink'><span datafld='Name'> </span></a></td>");
    document.write( "<td valign='top' bgcolor='#FFFFF F'><p align='left'><s pan datafld='ModelP ubName'></span></td>");
    document.write( "</tr>");
    document.write( "</tbody>");
    document.write( "</table>");
    }

    }[/CODE]
    Last edited by acoder; Sep 14 '07, 03:05 PM. Reason: Added code tags
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    try window.onload = function();

    swdata.filter = "" //this line can be removed

    Also you should create your table using DOM and then append it to the body
    Click here for a tutorial and information about dom elements. Also what is swdata

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Welcome to TSDN!

      Please use code tags when posting code:
      &#91;CODE=javas cript]Code goes here...[/CODE]

      Comment

      • clentoc
        New Member
        • Sep 2007
        • 2

        #4
        Originally posted by iam_clint
        try window.onload = function();

        swdata.filter = "" //this line can be removed

        Also you should create your table using DOM and then append it to the body
        Click here for a tutorial and information about dom elements. Also what is swdata
        Thanks for replying, tried the 2 quick fixes, but just the same result.
        I'll give DOM a try, but it looks a bit complex !

        swdata is a CSV table which contains links to other pages. I want to filter these links depending on the page the user is currently looking at. It would be a very efficient means of maintaining this data if I could get it to work !

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          From what you describe, it seems that the alert gives it time for loading.

          Run your code on page load. To do that you would put the code from line 15 into a function and call it on body load or window load.

          If you find DOM too hard, use innerHTML on a span or div, e.g.
          [HTML]<div id="test"></div>[/HTML] [CODE=javascript]document.getEle mentById("test" ).innerHTML = 'some data';[/CODE]You won't be able to use document.write after the page has loaded.

          Comment

          Working...