Tag: js

Timing Issues and Javascript

Sometimes, when you use javascript to make a change to some element on a page, you will get a Javascript error along the lines of: “element ‘n’ has no properties.” You check your page, and yes, the element is definitely there, and it is definitely named correctly, and your syntax is definitely correct. So where is the problem?

Well, it may be a timing issue. If your script executes before the element targeted by the script has been rendered, it will not find the element. This can particularly be a problem with frames or iframes. The solution is to move the function call to the bottom of the page, after the element has loaded.

Continue reading

Detect browser support for style properties

It is common to use object detection to discriminate between browser capabilities (See Quirksmode: Object Detection.

However, sometimes it is necessary to detect whether or not a browser understands a particular CSS style property, and it is not obvious how to do this.

You can’t test for the existence of the property directly but you can test for the data type of the value of the property. If the data type is anything but “undefined”, the property is understood by the browser.

The example below shows a couple of rows from an automatically generated table of items. Each row has a corresponding editable version which shows up when the “edit” button is clicked. The original, non-editable row is hidden at the same time the editable row is displayed. If “display=’block” is used to display the TR, Safari will render it as a TD. To correct this, “display=’table-row” must be used on browsers that understand it.

Continue reading

A Browser Sniffer/Screen Resolution Sniffer that swaps stylesheets

This is based on two javascripts, combined. This script contains a browser sniffer that will test for Mac or PC, and will also test if the PC is 800×600 or a higher resolution. I figure most macs are set to a decent resolution, but have found much to my dismay that many PC’s here are still set to 1997 standards.

This will swap style sheets depending on what is “sniffed”.

I’ve included the locations for the original scripts, which contain other options you may wish to include.

Continue reading

Javascript validation as a condition for a second function in the same event handler

We use Questionmark quizzing software at my job to create quizzes and tutorials. One of several issues we have had with it has been a login page which did not validate for the kind of ID number we wanted to use. In fact it did not validate at all! We were told that either it couldn’t be done because it would not work with the software (the login page submits some .asp variables), or it would cost us to have it done by their programmers – so we set about trying to make it happen.

The problem was that it uses the onSubmit event to trigger some fancy new window actions. I wanted the validation to take place and stop the new window action as well as the “submitting” action itself. So my first thought was to put a standard validation script on an onBlur event. In other words when people moved the cursor from the validated field to the next field, the validation would occur.

Continue reading