Chris Harshman

Navigation

Skip navigation.

Search

Site navigation

Style


(Uses cookies to store your choice)

Email conversation

FromChris Harshman
ToMe
SubjectWorking with Mutli-Broswer Code.
Date11 October 2007 06:11
Hello,

I recently read your tutorials, and I found them very good, I have a basic
to moderate understanding of javascript.

I have run into a problem, I am writing a script that displays a poll that
grabs data from a xml file.

My problem is with that fact that the code I have works only in IE, but I
took examples from other people that work in both and only changed the
variable names.

I have a testpage.htm which is were I test all my code, because it is better
to just have 1 file and then the xml file stored in a file folder called
data.

As of now, I am still to early in development to have the file hosted any
were, but I am only using javascript at the moment.

I have attached a zip file, with the testpage.htm and the /data/poll.xml

Basicly, I would like it, if you could to go through and see if you have had
any better luck firguring out why that code does not show in Firefox, but it
does in IE, I have the lastest broswer versions.

Also if you could tell em how to remove the button and replace it with the
value from the xml file? I am still working on this, but any help would be
great.

Thanks

P.S the function to load the xml document came from [URL], and I am
just using it.
FromMe
ToChris Harshman
SubjectRe: Working with Mutli-Broswer Code.
Date11 October 2007 10:14
Chris,

> Basicly, I would like it, if you could to go through and see if you have
> had any better luck firguring out why that code does not show in Firefox,
> but it does in IE, I have the lastest broswer versions.

Please check this in the browsers I ask you to on my contact page, and let
me know how they all respond. Then I will be in a much better position to
help you with the debugging.

http://www.howtocreate.co.uk/sendEmail.php#testbrowsers

> P.S the function to load the xml document came from w3schools.com, and I
> am just using it.

That method for loading XML has been dropped from all specifications. There
is a better way. I can go over that later once the main problem is  sorted
out.


Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
FromChris Harshman
ToMe
SubjectRe: Working with Mutli-Broswer Code.
Date11 October 2007 18:17
AttachmentFiles mentioned in the first email
Hello,

Ok I have tested on the Lastest IE, Firefox, Opera, Netscape, and the
question always shows on all broswers,
but the buttons do not. The buttons on IE are not clickable.

Thank you for offering to help me with loading the xml, I accept.

Thanks again
FromMe
ToChris Harshman
SubjectRe: Working with Mutli-Broswer Code.
Date11 October 2007 18:59
Chris,

> question always shows on all broswers, but the buttons do not.

OK, this shows it is probably because of an IE bug that your script expects.

Your script uses this:
option[0].childNodes[i-1].childNodes[0].nodeValue
But the XML structure is this:
 <option>
  <A>A</A>
  <B>B</B>
  ...
IE has a bug where it will ignore the whitespace between the elements.
Standards compliant browsers do not ignore it, and correctly create a text
node containing the whitespace in the DOM. I have documented this here:
http://www.howtocreate.co.uk/wrongWithIE/?chapter=Empty+Space

There are a few things you can do to fix it:
1. You can make sure you step through _all_ childNodes, and only process it
if it's nodeType property is 1.
2. You can remove all whitespace nodes from the XML file.
3. You can get the elements that are children of the options. Since there is
only one level of nesting, this should work:
option[0].getElementsByTagName('*')[i-1].childNodes[0].nodeValue
Note, however, that this does not work in IE 5.x.

> The buttons on IE are not clickable.

This seems to be a bug in IE. It refuses to create proper radio inputs. I
tried giving them names as well (radio inputs do not work properly without
name attributes), but it still fails. I do not know of any workaround for
this bug - I even tried other input types, but it seems it always ignores
them. I guess you will have to create the new HTML as a string and set it
using innerHTML, instead of using proper DOM. IE really sucks at DOM.

Separately though, you are using IDs inside a loop without changing them
each time. That gives several elements with the same ID, which is not
allowed.

> Thank you for offering to help me with loading the xml, I accept.

OK, what you were using for Firefox was part of a DOM Load and Save draft,
which was rejected and rewritten. Only Opera supports the new version.
However, _all_ current browsers support XMLHttpRequest instead. It is much
better to use that instead.

function loadXMLDoc(dname) {
  // Variables
  var xmlDoc;
  if( window.XMLHttpRequest ) {
    // code for all browsers except IE 6-
    xmlDoc = new XMLHttpRequest();
  } else if( window.ActiveXObject ) {
    // IE 6-
    xmlDoc = new ActiveXObject('Microsoft.XMLHTTP');
  } else {
    // no support :(
    return;
  }
  xmlDoc.open('GET',dname,false); //true for synchronous
  xmlDoc.send(null);
  return(xmlDoc.responseXML);
}

Note that IE 7 has seen fit to disable this when loading pages from the
local computer. If this is a problem, swap around the checks for
XMLHttpRequest and ActiveXObject, because its ActiveXObject version works
(but I prefer to put the standards branch first).

Note though that synchronous requests are a bad idea as they hang up the
browser waiting for a response. Most browsers will appear to have crashed
while this happens, if the response is not fast enough. If this is a
problem, switch to using asynchronous, and listen to the onreadystatechange
event on the xmlDoc object. When it fires, check if xmlDoc.readyState is 4,
and only then continue to process the document.
FromChris Harshman
ToMe
SubjectRe: Working with Mutli-Broswer Code.
Date11 October 2007 19:25
Hello,

Thank you for all you for all your help.

About the clickable, here is a link were generated buttons work in IE -
[URL]

That has bafled me, because as soon as I move the code it no longer works?
could it be something server side?

Thanks
FromMe
ToChris Harshman
SubjectRe: Working with Mutli-Broswer Code.
Date11 October 2007 20:14
Chris,

> That has bafled me, because as soon as I move the code it no longer works?

It seems they have a workaround, which is to pass HTML to createElement
instead of just passing an element name. This is invalid, but it works only
in IE.

Unfortunately the way they do it causes problems in browsers that follow the
spec, so I would prefer to do it the right way, check if it failed, _then_
use the IE way. I would do this by checking if the name attribute is there
after creating it, but it seems IE creates it and thinks it is there, but
does not treat it as part of the markup. So instead I will check the created
markup using an IE non-standard property, and see if the attribute was
created:
radio[i]=document.createElement('input');
radio[i].setAttribute('name','foo');
if( radio[i].outerHTML && !radio[i].outerHTML.match(/name/) ) {
  //redefine it so it works in IE
  radio[i]=document.createElement('<input name="foo">');
}
With this fix in there, it works.
FromChris Harshman
ToMe
SubjectRe: Working with Mutli-Broswer Code.
Date11 October 2007 21:09
Thank you yet again,

I had to tweak that code snippet by adding the type="radio" but it now
works.

Thank you for all your help.
FromChris Harshman
ToMe
SubjectRe: Working with Mutli-Broswer Code.
Date12 October 2007 00:58
Attachmentupdated files
Hello,

Sorry to bother you again.

It will not let me add what to do on click, as soon as I add that piece of
code it no longer loads in any of the 4 broswers I am using.

Thanks again.
FromChris Harshman
ToMe
SubjectRe: Working with Mutli-Broswer Code.
Date12 October 2007 01:00
Hello,

I just noticed a error is spelling with the one setAttribute, but I fixed
that and still nothing, I also tryed taking out the () at the end of
function call, and still nothing.

I have tryed to google it and I am still going through the results, you
might not read this til tommorrow, but i am working on it all night.

Thanks again.
FromChris Harshman
ToMe
SubjectRe: Working with Mutli-Broswer Code.
Date12 October 2007 01:20
Attachmentupdated files
Hello,

Ok so I have worked more on it, and have discovered that by using.

radio[i].onclick = 'onclickPoll()'; it will work with opera only, nothing
else seems to support this yet.

So you have any suggestions?

I am thinking I might need a sumbit button instead, which then can add to
the results.

Thanks

P.S here is the right files, with no spelling errors or mistakes.
FromMe
ToChris Harshman
SubjectRe: Working with Mutli-Broswer Code.
Date12 October 2007 08:21
Chris,

> radio[i].onclick = 'onclickPoll()'; it will work with opera only, nothing
> else seems to support this yet.

You cannot specify it as a string in most browsers. They expect a function
reference. The easiest way to do that is with an anonymous function:

radio[i].onclick = function () {
  alert('Now it should work');
  onclickPoll();
  alert('Finished');
};

Of course, since your event handler only runs one function when it fires,
you might as well just use a direct reference to that function:

radio[i].onclick = onclickPoll;

Note that there are no parenthesis () since you want to reference the
function, not run it.
FromChris Harshman
ToMe
SubjectRe: Working with Mutli-Broswer Code.
Date12 October 2007 16:27
Thank you so much for helping me, you have been a great help and have
actually made me enjoy self taught web programing more
I am more a windows game desginer, but it has be refreshing thank you again.

Thanks
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.