By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,647 Members | 1,604 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,647 IT Pros & Developers. It's quick & easy.

Javascript only works when Alert present

P: 2
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>EquivalenceTable()</script>

This is the code :

Expand|Select|Wrap|Line Numbers
  1. function EquivalenceTable() {
  2.  
  3. // Determines the path of the page
  4. var sPath = window.location.pathname;
  5.  
  6. // Sets up a databound table
  7. document.write("<object id='swdata' CLASSID='clsid:333C7BC4-460F-11D0-BC04-0080C7055A83' >");
  8. document.write("<param name='DataURL' value='/showcase1/OPD_Alpha_List/ee_nnw1.csv'>");
  9. document.write("<param name='UseHeader' value='True'>");
  10. document.write("<param name='CaseSensitive' value='False'>");
  11. document.write("</object>");
  12.  
  13. alert('Hello'); // REMOVE THIS AND CODE STOPS WORKING !
  14.  
  15. // Filter the table on the path value
  16.  swdata.filter = ""
  17.  swdata.filter = "Source = *" + sPath +"*";
  18.  swdata.sort = "ModelPubName" <!-- This sorts the results numerically --!>
  19.  swdata.reset()
  20.  var tableRecordCount = swdata.recordset.recordCount; // Counts the numbr of filtered records
  21.  
  22.  
  23. // Draws a table of filtered data if there are records to display
  24. if (tableRecordCount > 0 )
  25.   {
  26. document.write("</p>");
  27. document.write("<table border='0' width='50%' summary='Table of equivalent OPDs'cellspacing='1' bgcolor='#000000' id='swtable' datasrc='#swdata' cellpadding='3' datapagesize='25' id='datatable'>");
  28. document.write("<thead>");
  29. document.write("<th bgcolor='#CC3300' width='50%'><p align='center'><a><font color='black'><strong>Equivalent OPD(s)</strong></font></a></p></th>");
  30. document.write("<th bgcolor='#F5CB99' width='70%'><p align='center'><a><font color='black'><strong>Processing Area</strong></font></a></p></th>");
  31.  
  32. document.write("</thead>");
  33. document.write("<tbody>");
  34. document.write("<tr bgcolor='#F5CB99' valign='top'>");
  35. document.write("<td valign='top' bgcolor='#FFFFFF' align='left'><a datafld='Hyperlink'><span datafld='Name'></span></a></td>");
  36. document.write("<td valign='top' bgcolor='#FFFFFF'><p align='left'><span datafld='ModelPubName'></span></td>");
  37. document.write("</tr>");
  38. document.write("</tbody>");
  39. document.write("</table>");
  40. }
  41.  
  42. }
Sep 14 '07 #1
Share this Question
Share on Google+
4 Replies


iam_clint
Expert 100+
P: 1,208
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
Sep 14 '07 #2

acoder
Expert Mod 15k+
P: 16,027
Welcome to TSDN!

Please use code tags when posting code:
[CODE=javascript]Code goes here...[/code]
Sep 14 '07 #3

P: 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
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 !
Sep 17 '07 #4

acoder
Expert Mod 15k+
P: 16,027
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]
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("test").innerHTML = 'some data';
You won't be able to use document.write after the page has loaded.
Sep 18 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.