This topic is locked

Inconsistent problems with JavaScript OnLoad Event in Edit Page

11/8/2011 1:02:46 PM
PHPRunner General questions
P
psiconsultants author

We have created a simple application that does an ROI calculator. The first field is a drop down list from a lookup table and it populates the 2nd field with a numeric value. Otherwise, the calculations are very simple. I used a sample JavaScript OnLoad event that takes about 4 numeric fields and multiples them together. In some cases, it works exactly as we expect. But different browsers will sometimes work, and sometimes not.
A few comments:

  1. We are using a completely custom edit page and not using the default edit page layout. We simply inserted the fields and designed our own page.
  2. We've had it work with some IE 9 and some don't work with IE 9.
  3. We've had it work with some IE 8 and some don't work with IE 8.
  4. If you hit "refresh", then IE will work. (but obviously this isn't ideal!)
  5. The calculations and Javascript seem to always work perfectly with Chrome and Firefox, however the formatting of the text does not work (i.e. bolding or size of text).
    We suspect that because we get it to work and fail on similar versions of IE, this might be related to "compatibility viewing" settings? Or maybe security settings? Also, it is strange that it sometimes fails on the initial load, but if you hit refresh it then works. It always works with Chrome and Firefox, but formatting does not?!?!
    Thanks in advance for any insight!

C
cgphp 11/8/2011

Post the JS code.

P
psiconsultants author 11/8/2011



Post the JS code.


The following is in the "JavaScript OnLoad Event" section of the "Edit page" of my table.

...
// Place event code here.

// Use "Add Action" button to add code snippets.
var ctrlAssetType = Runner.getControl(pageid,'Asset_Type');

var ctrlFuelSavings = Runner.getControl(pageid, 'Fuel_Savings');

var ctrlAssetsAffected = Runner.getControl(pageid, 'Assets_Affected');

var ctrlFuelUse = Runner.getControl(pageid, 'Fuel_Use');

var ctrlFuelCost = Runner.getControl(pageid, 'Fuel_Cost');

var ctrlSavingsMonth = Runner.getControl(pageid,'Savings_per_Month');

var ctrlSavingsYear = Runner.getControl(pageid,'Savings_per_Year');

var ctrlAssetCount = Runner.getControl(pageid, 'Asset_count');
function func() {
ctrlSavingsMonth.setValue(Math.round(parseFloat(ctrlAssetCount.getValue()) parseFloat(ctrlFuelSavings.getValue()) parseFloat(ctrlAssetsAffected.getValue()) parseFloat(ctrlFuelUse.getValue()) parseFloat(ctrlFuelCost.getValue()) ));

ctrlSavingsYear.setValue(Math.round(parseFloat(ctrlAssetCount.getValue()) parseFloat(ctrlFuelSavings.getValue()) parseFloat(ctrlAssetsAffected.getValue()) parseFloat(ctrlFuelUse.getValue()) parseFloat(ctrlFuelCost.getValue()) * 12 ));
};
ctrlAssetType.on('keyup', func);

ctrlAssetCount.on('keyup', func);

ctrlFuelSavings.on('keyup', func);

ctrlAssetsAffected.on('keyup', func);

ctrlFuelUse.on('keyup', func);

ctrlFuelCost.on('keyup', func);

C
cgphp 11/8/2011

Do you get some error in firebug ? What if no value is set for a field (null value) ?

Admin 11/8/2011

A quick suggestion. parseFloat() function won't be able to handle empty or null values which means you have to populate all fields in order to perform calculations. I would suggest replacing parseFloat() with + that will deal with empty values without breaking.

function func() {
ctrlSavingsMonth.setValue(Math.round(+(ctrlAssetCount.getValue()) * +(ctrlFuelSavings.getValue()) * +(ctrlAssetsAffected.getValue()) * +(ctrlFuelUse.getValue()) * +(ctrlFuelCost.getValue()) ));

ctrlSavingsYear.setValue(Math.round(+(ctrlAssetCount.getValue()) * +(ctrlFuelSavings.getValue()) * +(ctrlAssetsAffected.getValue()) * +(ctrlFuelUse.getValue()) * +(ctrlFuelCost.getValue()) * 12 ));
};
P
psiconsultants author 11/8/2011

Thanks for the suggestions. There actually aren't any missing values as it is being forced to record 1 of the table and all fields are populated. All fields are also required. (although the validation doesn't work either when the page fails. Makes sense I guess if the javascript is failing on other stuff.) I actually also initially had some code to check for missing values before calling the function just in case, but I removed it temporarily to eliminate it as my problem. But I'm pretty sure missing values aren't causing the problem. I did try Sergey's code snippet anyway and I get the same exact results.
I uploaded the example to the demo site, and can also email you with the site on my server that has been opened up through the firewall. I'm curious to see if your browsers get the same results (i.e. error). I still get the error with some browsers with the code uploaded to the demo site so I've eliminated any problems on my server. I don't want to post the link to my server publicly but if you send me your email, I'll send you the link so you can see the behavior.
One other thing... the first variable is a drop down list and the second variable is a drop down list that is dependent on the selection of the first. I don't think this is causing the problem as it again works fine on some browsers but not others. But the instances where a browser doesn't work on the initial entry to the site, they do work once you hit the browser refresh button.
Still scratching my head...
p.s. I haven't tried to debug with Firebug, but I will try to check that out. I don't think I mentioned this before, but I'm also using PHPRunner v6.
Thanks so much for your feedback and suggestions.
Regards

P
psiconsultants author 11/8/2011

I installed firebug, but the javascript does't fail in firefox. However this does let me track down my cosmetic problem.
It does look like when I inspect items firebug, most everything is working correctly with the exception of the <strong> tag that PHPRunner uses when I'm bolding text. I can see the tag is correctly written, but Firefox doesn't bold the text. Does firefox not support this tag? Otherwise I think everything else is okay with this browser.
So I've narrowed the javascript problem down to some IE versions. Is there an equivalent application for IE that would help me debug this?
Thanks,

C
cgphp 11/8/2011

keyup is not the right event for a dropdown. Use change instead:

ctrlAssetType.on('change', func);
Admin 11/8/2011

Similar developer tool is available in IE, press F12 to open it. Not sure what was the first version where they started shipping.

P
psiconsultants author 11/8/2011

My version of IE does have the debugging available but I'm not sure what to look for as I'm not even familiar with javascript. But, I saw one more interesting thing... I can actually reproduce the problem in the SAME browser. If I keep hitting refresh, it will load correctly sometimes, and other times it will fail. I initially thought it was happening with some browsers and not others, but it's actually having problems in the same browser sporadically.
My WAMP and PHP code is on a low end server and my simple table of about 8 fields and 1 record will sometimes take 5 or 6 seconds to load. Do you think the inconsistent "it works / it doesn't work" in the same browser and session could be related to something not loading fast enough and the javascript trying to run before everything is ready? (grasping at straws here)
I also updated the first two fields to be 'change' rather than 'keyup' to match the drop down list type. Still get the same results...

ctrlAssetType.on('change', func);

ctrlAssetCount.on('change',func);

Admin 11/8/2011

It does look like the case of page not being loaded completely and thus Javascript OnLoad event is never fired.
Just in case try to upload this app to any other server i.e. to Demo Account to see if this makes any difference.

P
psiconsultants author 11/9/2011



It does look like the case of page not being loaded completely and thus Javascript OnLoad event is never fired.
Just in case try to upload this app to any other server i.e. to Demo Account to see if this makes any difference.


I can't replicate the error on your demo server and it runs pretty fast. I'm thinking this may be related to the old web server we are running and slow performance. Maybe IE is having latency problems or something.
I am going to do a fresh WAMP install on a different machine and copy the application over. Fingers crossed that this resolves my problem. I'll let you know what happens.
Thanks!

P
psiconsultants author 11/10/2011

Upon further review, the problem actually is reproduced on the asprunner.net demo server. It only happens with IE, but not Firefox or Chrome.

A
acpan 11/10/2011



Upon further review, the problem actually is reproduced on the asprunner.net demo server. It only happens with IE, but not Firefox or Chrome.


Hi,
could your problem related to my problem?
http://www.asprunner.com/forums/topic/18210-mysterious-out-of-alignment-problem-for-some-ie-browsers/
For my case, it seems that for some IE (even it's version 7 or 8), it causes my problem above. i.e. the

page misaligned, whenever i have echo command at before process event. Strangely, it works for some other

IE. So there is no fix pattern.
And i realized that in addition to before process event, if i have javascrpit echo alert propmting user to click ok

at the after record added event, it created the same problem too. Because of this, my workaround mentioned in the link above,

is not good enough to fix this because i have javascript alerts in many places.
Was suspecting some javascript library not able to load fully specifically for some IE browsers but i have no clue at all.

It seems very tricky.
If you can verify that my problem can be replicate to your environment, it is probably that your IE browser environment

fit into this scenario.
fyi, i am using good HP server with latest php and apache. For now, i advise my users that has this problem use website generated by

phpr 5.3 instead. Hope Sergey and the team may be able to provide a solution, if the problem is something that can be fixed.
acp.

Admin 11/10/2011

psiconsultants,
What's the URL of your app on Demo Account and what version of IE I need to use in order to reproduce it?