By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,377 Members | 1,690 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,377 IT Pros & Developers. It's quick & easy.

IE6 specific Object Doesn't Support ... Error

100+
P: 103
Hi All

I'm using AJAX in an analytics script. All works nicely in Firefox and the script loads my php script which records some information in the database.

The part with the error in IE is the part which adds the events to the anchor elements. The error message from IE is:

Line 335: Object doesn't support this property or method.

The javascript line this refers to (I have confirmed this 100% even though line ref is wrong) is as follows:

Expand|Select|Wrap|Line Numbers
  1. links = document.getElementsByTagName('a');
  2.  
As you can imagine I've tried looking for standard IE bugs for this type of line but nothing comes up.

Some thoughts I've had: it could be using "links" to name a variable (is links reserved)? It could be that "a" has to be "A". I have tried different combinations of these but no luck.

Hopefully someone can point me in the right direction!

Thanks in advance!

Henry
Nov 8 '07 #1
Share this Question
Share on Google+
7 Replies


acoder
Expert Mod 15k+
P: 16,027
That line shouldn't cause errors. Can you post the rest of your code?
Nov 8 '07 #2

100+
P: 103
Hi

Thanks for helping out. I thought that might be the case. The relevant code is below. Hopefully it yields some answers...

Expand|Select|Wrap|Line Numbers
  1. /-------------------ANALYTICS--------------------------//
  2. var xmlHttp; //ajax object
  3. var vapk; //the visitor pk which is retrieved on entry and used on exit to update
  4. var listeners = []; //array
  5.  
  6. function listenAllLinks() {
  7.   if (!document.getElementsByTagName) return false;
  8.   // find links in document
  9.   aLinks = document.getElementsByTagName('a');
  10.  
  11.   // if link does have a id add one
  12.   for (var i = 0; i < aLinks.length; i++) {
  13.     addEvent( aLinks[i], 'click', exit, false );
  14.     addEvent( aLinks[i], 'keypress', linkKeyPress, false );
  15.   }
  16.  
  17.   // find buttons in document
  18.   inputs = document.getElementsByTagName('input');
  19.   for (var i = 0; i < inputs.length; i++) {
  20.     type = inputs[i].getAttribute('type');
  21.     // only attach events to buttons
  22.     if ( type == 'submit' || type == 'button' ) {
  23.       addEvent( inputs[i], 'click', exit, false );
  24.       addEvent( inputs[i], 'keypress', linkKeyPress, false );
  25.     }
  26.   }
  27. }
  28.  
  29. function linkKeyPress(e) {
  30.   // check for return key press
  31.   var keyID = (window.event) ? event.keyCode : e.keyCode;
  32.   if (keyID == 13) {
  33.     exit(e);
  34.   }
  35. }
  36.  
  37. function addEvent( thisE, evType, fn, useCapture ) {
  38.   // Updated version which captures passed events
  39.   if (thisE.AddEventListener) {
  40.     thisE.AddEventListener(evType, fn, useCapture);
  41.     return true;
  42.   }
  43.   else if (thisE.attachEvent) {
  44.     var r = thisE.attachEvent('on' + evType, fn);
  45.     window.listeners[window.listeners.length] = [ thisE, evType, fn ];
  46.     return r;
  47.   }
  48.   else {
  49.     var xEventFn = thisE['on' + evType];
  50.     if (typeof thisE['on' + evType] != 'function')    {
  51.       thisE['on' + evType] = fn;
  52.     }
  53.     else {
  54.       thisE['on' + evType] = function(e) { xEventFn(e); fn(e); };
  55.     }
  56.   }
  57. }
  58.  
  59. function findSourceElement(e) {
  60.   // finds event source
  61.   if (typeof e == 'undefined') {
  62.     var e = window.event;
  63.   }
  64.  
  65.   var source;
  66.   if (typeof e.target != 'undefined') {
  67.     source = e.target;
  68.   }
  69.   else if (typeof e.srcElement != 'undefined') {
  70.     source = e.srcElement;
  71.   }
  72.   else {
  73.     return true;
  74.   }
  75.   if (source.nodeType == 3) {
  76.     source = source.parentNode;
  77.   }
  78.   return source;
  79. }
  80.  
  81. function getFormTarget( elt ) {
  82.   // returns the form action attribute from
  83.   // if given the child node of that form
  84.   target = null;
  85.   parentElt = elt.parentNode;
  86.   if( parentElt.nodeType == 1 ) {
  87.     if( parentElt.tagName == 'FORM' ) {
  88.       target = parentElt.getAttribute('action');
  89.     }
  90.     else {
  91.       target = getFormTarget( elt.parentNode );
  92.     }
  93.   }
  94.   else {
  95.     target = getFormTarget( elt.parentNode );
  96.   }
  97.   return target;
  98. }
  99.  
  100. function exit(e) {
  101.   // records click information using ajax
  102.   source = findSourceElement(e);
  103.   tag = source.tagName;
  104.   var id,label,target
  105.  
  106.   if( tag == 'IMG') {
  107.     if( source.parentNode.tagName == 'A' ) {
  108.       target = source.parentNode.href;
  109.     }
  110.   }
  111.  
  112.   if( tag == 'A' ) {
  113.     target = source.href;
  114.   }
  115.  
  116.   if( tag == 'INPUT' ) {
  117.     return true;
  118.     if( source.getAttribute('type') == 'submit' ) {
  119.       target = getFormTarget( source );
  120.     }
  121.     else {
  122.       target = 'script';
  123.     }
  124.   }
  125.  
  126.   if (window.vapk) {
  127.     xmlHttp=GetXmlHttpObject();
  128.     if (xmlHttp==null) {
  129.       //alert ("Browser does not support HTTP Request")
  130.       return true;
  131.     }
  132.  
  133.     var script= "ajax/analytics_exit.php";
  134.     target = escape( target );
  135.     var url=script+"?vapk="+window.vapk+"&exit_page="+target+"&rand="+Math.random();
  136.  
  137.     xmlHttp.onreadystatechange=stateChanged;
  138.     xmlHttp.open("GET",url,true);
  139.     xmlHttp.send(null);
  140.   }
  141.   return true;
  142. }
  143.  
  144. function entry(ref,uri) {
  145.   //alert("entry running now");
  146.   xmlHttp=GetXmlHttpObject();
  147.   if (xmlHttp==null) {
  148.     //alert ("Browser does not support HTTP Request")
  149.     return
  150.   }
  151.  
  152.   var sid= "<?php echo (isset($_COOKIE['PHPSESSID'])) ? $_COOKIE['PHPSESSID'] : NULL; ?>";
  153.   var cid= "<?php echo (isset($_SESSION['cid'])) ? $_SESSION['cid'] : 0; ?>";
  154.   var ip= "<?php echo $_SERVER['REMOTE_ADDR']; ?>";
  155.   var res= screen.width + "x" + screen.height;
  156.   var agent= "<?php echo $_SERVER['HTTP_USER_AGENT']; ?>";
  157.   if (ref==undefined) {
  158.     ref = 'direct';
  159.   }
  160.   if (uri==undefined) {
  161.     uri = 'unknown';
  162.   }
  163.  
  164.   var script= "ajax/analytics_entry.php";
  165.  
  166.   var url=script+"?cid="+cid+"&ip="+ip+"&res="+res+"&agent="+agent+"&ref="+ref+"&uri="+uri+"&sid="+sid;
  167.  
  168.   xmlHttp.onreadystatechange=stateChanged;
  169.   xmlHttp.open("GET",url,true);
  170.   xmlHttp.send(null);
  171. }
  172.  
  173. function stateChanged() {
  174.   if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
  175.     var r=xmlHttp.responseText;
  176.     if (!isNaN(r)) {
  177.       window.vapk=r;
  178.       return;
  179.     }
  180.     else if (r.indexOf('http://')==0) {
  181.       window.location=r;
  182.     }
  183.     else {
  184.       return;
  185.     }
  186.   }
  187. }
  188.  
  189. function GetXmlHttpObject() {
  190.   var xmlHttp=null;
  191.   try { // Firefox, Opera 8.0+, Safari
  192.     xmlHttp=new XMLHttpRequest();
  193.   }
  194.   catch (e) { //Internet Explorer
  195.     try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
  196.     catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
  197.   }
  198.   return xmlHttp;
  199. }
  200.  
  201. //------------end analytics------------//
  202.  
Thanks again!
Nov 9 '07 #3

acoder
Expert Mod 15k+
P: 16,027
Can you also post the corresponding HTML?
Nov 9 '07 #4

100+
P: 103
Hi acoder

There isn't really any corresponding HTML as such but here is all the information I can think relates to the analytics functions.

The javascript is loaded by the following:

Expand|Select|Wrap|Line Numbers
  1. <body onload="load()">
  2.  
Expand|Select|Wrap|Line Numbers
  1. //external
  2. function load() {
  3.   listenAllLinks();
  4. }
  5.  
The pages contain hundreds of links all in standard format: <a href="..." title="...">...</a>.

The idea is that the listenAllLinks() function adds events to all the anchor elements. When somone clicks on the anchor element, the database is updated with the click recorded.

I call the entry() function on page load (on an inline script) to record the visit after the page has loaded.

Hope that gives all the information. I'm a little mystified by the problem, hence asking for help!

Thanks again!

Henry
Nov 9 '07 #5

acoder
Expert Mod 15k+
P: 16,027
So this happens when adding the events, right?

Check your form ids that they're not reserved words, etc.
Nov 9 '07 #6

100+
P: 103
after changing
Expand|Select|Wrap|Line Numbers
  1. links= document.getElementsByTagName('a');
  2.  
to
Expand|Select|Wrap|Line Numbers
  1. aLinks= document.getElementsByTagName('a');
  2.  
the script now works! I don't believe it - this bug has been causing me headaches for months.

Simple is normally the explanation. Thanks for your help acoder!

Henry
Nov 9 '07 #7

acoder
Expert Mod 15k+
P: 16,027
Glad you got it working.

Check your code for elements named "links". IE 6 is easily confused!
Nov 9 '07 #8

Post your reply

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