473,382 Members | 1,252 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

This is the code :

Expand|Select|Wrap|Line Numbers
  1. function EquivalenceTable() {
  3. // Determines the path of the page
  4. var sPath = window.location.pathname;
  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>");
  13. alert('Hello'); // REMOVE THIS AND CODE STOPS WORKING !
  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
  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>");
  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. }
  42. }
Sep 14 '07 #1
4 4583
1,208 Expert 1GB
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
16,027 Expert Mod 8TB
Welcome to TSDN!

Please use code tags when posting code:
[CODE=javascript]Code goes here...[/code]
Sep 14 '07 #3
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
16,027 Expert Mod 8TB
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

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

Similar topics

by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
by: Vincent van Beveren | last post by:
Hi everyone, I have the following code using inheritence The important part is the function getName(). The result should alert 'John Doe': function First(name) { this.name = name; ...
by: tshad | last post by:
I posted this on the asp.net group, also. I wasn't sure whether this was an asp.net problem or a javascript problem. I have a page that was originally created from a program I found on the net...
by: tshad | last post by:
This is from my previous post, but a different issue. I have the following Javascript routine that opens a popup page, but doesn't seem to work if called from an asp.net button. It seems to work...
by: Roman Ziak | last post by:
Hello, there were times when I used to be looking for a way to access JavaScript Global object similar to those found in VBScript or PHP ($GLOBALS). At present this has only academic value for...
by: Phlip | last post by:
Javascripters: I have an outer page and an inner iframe. The outer page calculates some javascript, and wants the inner frame to run it. The inner frame should hit a page on the same (private)...
by: thatsMaBoy | last post by:
Hi, I am attempting to display a simple Javascript alert when a webpage loads. It works on some pages but not on others. For example, the following code does not produce an alert: Code in...
by: marss | last post by:
Maybe anyone know good free online JavaScript knowledge test? This not exactly a system for testing online required - it may be simply list of questions with variants of answers (I have to prepare...
by: Fabio Mastria | last post by:
Looks like a joke... : As I say in the subject... I create a javascript file, with this function: function IndexChanged(source){ alert("Hallo");
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.