473,387 Members | 1,904 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,387 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() {
  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
4 4584
iam_clint
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
acoder
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
acoder
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

5
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...
11
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; ...
14
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...
9
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...
16
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...
15
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)...
5
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...
60
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...
7
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");
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
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
0
BarryA
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...
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.