View Full Version : Script bugging DHTML using DOM
TriMoon
02-01-2005, 06:52 AM
When i let my application create the script code inside a DOM ovject it cuases my whole page to clear out.
How can i still get the functionality without this buggy behaviour?
PS: its not my code thats causing the bug, its the commands inside your script!
See my homepage for the DHTML application where i want to use it.
webado
02-01-2005, 06:58 AM
You may need to generate the version of the code for xhtml and make sure you don't break it across lines when you insert it.
TriMoon
02-01-2005, 03:27 PM
I think you dont understand what it means to generate DHTML using Javascript and DOM...
webado
02-01-2005, 04:47 PM
I think you dont understand what it means to generate DHTML using Javascript and DOM...
I definitely don't know what you DOM thing is :lol:
TriMoon
02-02-2005, 03:34 AM
In that case have a read about it here at W3C-DOM (http://www.w3.org/DOM/DOMTR)
FYI, My application uses DOM2, with backsteps to DOM1 for browsers who are not compatible with DOM2 like IE...
PS: Why no one from statcounter replies to my bug?
TriMoon
02-03-2005, 11:09 PM
FYI this is the code i used to generate the code using DOM in JavaScript:
// Create an script element. (var init)
DomObj[0] = document.createElement("script");
DomObj[0].setAttribute("type", "text/javascript");
DomObj[1] = document.createTextNode("<!--");
DomObj[0].appendChild( DomObj[1] );
DomObj[1] = document.createTextNode("var sc_project=541068;");
DomObj[0].appendChild( DomObj[1] );
DomObj[1] = document.createTextNode("var sc_partition=3;");
DomObj[0].appendChild( DomObj[1] );
DomObj[1] = document.createTextNode("//-->");
DomObj[0].appendChild( DomObj[1] );
oTD.appendChild( DomObj[0] );
// Create an script element. (counter script)
DomObj[0] = document.createElement("script");
DomObj[0].setAttribute("type", "text/javascript");
// DomObj[0].setAttribute("src", "http://www.statcounter.com/counter/frames.js");
DomObj[0].setAttribute("src", "http://www.statcounter.com/counter/counter_xhtml.js");
oTD.appendChild( DomObj[0] );
webado
02-04-2005, 01:16 AM
Do YOU know what you're missing or what is wrong? If you posted a link to a page which suffers from some bug you think you found in the Statcounter code, please post it. It's impossible to evaluate otherwise.
Have you looked at the code that is generated by this on your page to see in which way it interferes with your other stuff?
The Statcounter code works fine in regularly rendered html pages where you insert the appropriate version just as it is. Whatever programming language you may use has got to generate html code that is exactly as the one to be inserted otherwise.
You have to take into account yourself the limitations and the restrictions of construct of your application.
I have yet to find an actual bug that could be attributed to the Statcounter code, always usage error, and I'm referring to the last several months of using it. You may debate the stats on the reporting side, but not the code itself that serves simply to log information.
Why no staff answers your bug? If you feel it's a real bug, you should open a support ticket. Otherwise you post it in the forum like you did and let others discuss and try to help if they can.
Obviously I cannot help you.
TriMoon
02-04-2005, 02:13 AM
The bug is in the Statcounter script 100%.
Why?
Because it uses "document.write()" to insert code into your page.
This is normaly ok when using regular HTML/XHTML but not when the contents of the page is generated using the DOM interface.
So what ppl like me need is a DOM version of the script.
For now i have dug into the script that gets loaded and incorporated the actual used code into my DOM-Application.
I simply could not wait for others to help as obviously DOM is to new a technology for this project,
i even asked for parameters and no one replied to it.
for those that are interested here is the actual code i incorporated into my application.
People who are familiar with DOM will know to adjust it to their needs.
Note: I generate the current page location each time it gets used as the application shows a different info page when the image is to be shown (footer)
Good luck all and bb4now
// Create an anchor element.
DomObj[0] = document.createElement("a");
DomObj[0].setAttribute("href", "http://my.statcounter.com/project/standard/stats.php?project_id=541068&guest=1");
var sCurLoc, tmpImg = new Image();
sCurLoc = document.location.protocol + "//" + document.location.host;
sCurLoc += document.location.pathname.substring(0, 1+document.location.pathname.lastIndexOf('/'));
sCurLoc += Universe.Control.MakeSearch();
sText = "http://c4.statcounter.com/t.php?";
sText += "sc_project=" + 541068;
sText += "&resolution=" + screen.width;
sText += "&camefrom=" + escape(document.referrer.substring(0, 150));
sText += "&u=" + escape(sCurLoc);
sText += "&t=" + escape(document.title.substring(0, 150));
sText += "&java=1";
sText += "&sc_random=" + Math.random();
// sText = "http://c4.statcounter.com/counter.php?sc_project=541068&java=0"
tmpImg.src = sText;
// Create an img element.
DomObj[1] = document.createElement("img");
DomObj[1].setAttribute("src", sText);
DomObj[1].setAttribute("alt", "StatCounter - Click for stats");
DomObj[1].setAttribute("title", "StatCounter - Click for stats");
if(isIE4||isIE5||isIE6){
DomObj[1].width = 58;
DomObj[1].height = 17;
};
DomObj[0].appendChild( DomObj[1] );
oTD.appendChild( DomObj[0] );
webado
02-04-2005, 02:56 AM
You are aware that by using this code you may be breaking the TOS of Statcounter, are you not?
If anything in the javascript code scurrently stored on the Statcounter servers gets modified you will not have those modifications reflected in your code either.
The code works as javascript and/or html. If DOM cannot handle either of those situations, then the code is not for you and this application.
TriMoon
02-04-2005, 04:15 AM
1st of all I am not breaking any of the Statcounter T.O.S. as i am not modifing the code in their scripts.
2nd why you think i posted a bug report asking for a DOM version?
If they disable my counter because i am using this code while they dont provide a DOM version then so be it and i will just move on to a different counter.
Afterall this is not the only one on the web...
bediviere
05-20-2005, 04:11 AM
The bug is in the Statcounter script 100%.
Why?
Because it uses "document.write()" to insert code into your page.
This is normaly ok when using regular HTML/XHTML but not when the contents of the page is generated using the DOM interface.
So what ppl like me need is a DOM version of the script.
I'm running into the same problem. It's really unfortunate that Statcounter uses "document.write()" this way. I take it there's still no word on an official DOM compatible version of the Statcounter script?
bediviere
05-20-2005, 05:09 AM
I thought that maybe I could help by offering a conversion of the counter_xhtml.js script:
function get_statcounter_dom() {
var sc_width=screen.width;
var sc_referer = ""+document.referrer;
// var sc_referer = parent.document.referrer;
var sc_os = "";
var sc_title = "";
var sc_url = "";
var sc_unique = 0;
var sc_returning = 0;
var sc_returns = 0;
var sc_base_dir;
var sc_error=0;
var sc_remove=0;
var sc_http_url="http";
var sc_security_code = "";
if(window.sc_https) {
if(sc_https==1) {
sc_doc_loc = ''+document.location;
myRE = new RegExp("^https", "i")
if(sc_doc_loc.match(myRE)) {
sc_http_url = "https";
}
else {
sc_http_url = "http";
}
}
else
sc_http_url = "http";
}
if(window.sc_partition) {
if(sc_partition==1)
sc_base_dir = sc_http_url+"://c2.statcounter.com/";
else if(sc_partition==2)
sc_base_dir = sc_http_url+"://c3.statcounter.com/";
else if(sc_partition==3)
sc_base_dir = sc_http_url+"://c4.statcounter.com/";
else if(sc_partition==4)
sc_base_dir = sc_http_url+"://c5.statcounter.com/";
else if(sc_partition==5)
sc_base_dir = sc_http_url+"://c6.statcounter.com/";
else if(sc_partition==6)
sc_base_dir = sc_http_url+"://c7.statcounter.com/";
else
sc_base_dir = sc_http_url+"://c1.statcounter.com/";
}
else {
sc_base_dir = sc_http_url+"://c1.statcounter.com/";
var sc_partition=0;
}
if(window.sc_text)
sc_base_dir += "text.php?";
else
sc_base_dir += "t.php?";
if(window.sc_project) {
sc_base_dir += "sc_project="+sc_project;
//if((sc_project==242990)||(sc_project==238850)||(sc _project==244973)||(sc_project==231320)||(sc_proje ct==238917)|| (sc_project==237927)||(sc_project==192888)||(sc_pr oject==237378))
// sc_remove=1;
}
else if(window.usr) {
sc_base_dir += "usr="+usr;
}
else {
sc_error = 1;
}
if(window.sc_remove_link) {
sc_link_back_node = null;
}
else {
sc_link_back_node = document.createElement('a');
sc_link_back_node.className = "statcounter"
sc_link_back_node.setAttribute("href", "http://www.statcounter.com");
sc_link_back_node.setAttribute("target", "_blank");
}
sc_date = new Date();
sc_time = sc_date.getTime();
sc_time_difference = 60*60*1000;
sc_title = ""+document.title;
sc_url = ""+document.location;
sc_referer = sc_referer.substring(0, 150);
sc_title = sc_title.substring(0, 150);
sc_url = sc_url.substring(0, 150);
sc_referer = escape(sc_referer);
sc_title = escape(sc_title);
sc_url = escape(sc_url);
if (window.sc_security) {
sc_security_code = sc_security;
}
var sc_tracking_url = sc_base_dir+"&resolution="+sc_width+"&camefrom="+sc_referer+"&u="+sc_url+"&t="+sc_title+"&java=1&security="+sc_security_code+"&sc_random="+Math.random();
// if no usr or project set then display visibile
// if usr=="someuser" then display visibile
if(sc_error==1) {
return document.createTextNode("Code corrupted. Insert fresh copy.");
}
else if(sc_remove==1) {
var remove_message = "StatCounter cannot track a high volume website like yours for free. This was stated several times during the sign up process. Please remove the code ASAP."
var b_node = document.createElement('b');
b_node.appendChild(document.createTextNode(remove_ message));
return b_node;
}
//else if(sc_partition==3) {
// down at the moment
//}
else if (window.sc_text) {
var script_node = document.createElement("script");
script_node.setAttribute("language", "JavaScript");
script_node.setAttribute("src", sc_tracking_url);
return script_node;
}
else if (window.sc_invisible == 1) {
return document.createTextNode("");
}
else {
var div_node = document.createElement('div');
div_node.className = "statcounter";
img_node = document.createElement('img');
img_node.setAttribute("src", sc_tracking_url);
img_node.setAttribute("alt", "StatCounter - Free Web Tracker and Counter");
img_node.setAttribute("border", "0");
if (sc_link_back_node == null) {
div_node.appendChild(img_node);
} else {
sc_link_back_node.appendChild(img_node);
div_node.appendChild(sc_link_back_node);
}
return div_node;
}
}
It doesn't quite work; while I get exactly the same DOM nodes as I do if I use the normal copy/paste methodology, I get an "Invalid Project ID" image in the one created as above. Anyway, to avoid violating the TOS, I'm not actually planning on using this code, but I thought that it might be a helpful step towards Statcounter supporting the DOM standard.
In case a diff is easier to read, here it is:
--- counter_xhtml_old.js 2005-05-19 23:06:29.015625000 -0600
+++ counter_xhtml.js 2005-05-19 23:06:52.968750000 -0600
@@ -1,3 +1,5 @@
+function get_statcounter_dom() {
+
var sc_width=screen.width;
var sc_referer = ""+document.referrer;
// var sc_referer = parent.document.referrer;
@@ -11,8 +13,6 @@
var sc_error=0;
var sc_remove=0;
var sc_http_url="http";
-var sc_link_back_start = "";
-var sc_link_back_end = "";
var sc_security_code = "";
if(window.sc_https) {
@@ -69,12 +69,13 @@
}
if(window.sc_remove_link) {
- sc_link_back_start = "";
- sc_link_back_end = "";
+ sc_link_back_node = null;
}
else {
- sc_link_back_start = "<a class=\"statcounter\" href=\"http://www.statcounter.com\" target=\"_blank\">";
- sc_link_back_end = "<\/a>";
+ sc_link_back_node = document.createElement('a');
+ sc_link_back_node.className = "statcounter"
+ sc_link_back_node.setAttribute("href", "http://www.statcounter.com");
+ sc_link_back_node.setAttribute("target", "_blank");
}
sc_date = new Date();
@@ -103,26 +104,40 @@
if(sc_error==1) {
- document.writeln("Code corrupted. Insert fresh copy.");
+ return document.createTextNode("Code corrupted. Insert fresh copy.");
}
else if(sc_remove==1) {
- document.writeln("StatCounter cannot track a high volume website like yours for free. This was stated several times during the sign up process. Please remove the code ASAP.");
+ var remove_message = "StatCounter cannot track a high volume website like yours for free. This was stated several times during the sign up process. Please remove the code ASAP."
+ var b_node = document.createElement('b');
+ b_node.appendChild(document.createTextNode(remove_ message));
+ return b_node;
}
//else if(sc_partition==3) {
// down at the moment
//}
else if (window.sc_text) {
- document.writeln('<scr' + 'ipt language="JavaScript"' + ' src=' + sc_tracking_url + '></scr' + 'ipt>');
+ var script_node = document.createElement("script");
+ script_node.setAttribute("language", "JavaScript");
+ script_node.setAttribute("src", sc_tracking_url);
+ return script_node;
}
-else if (window.sc_invisible) {
- if(window.sc_invisible==1) {
- sc_img = new Image();
- sc_img.src = sc_tracking_url;
- }
- else {
- document.writeln("<div class=\"statcounter\">"+sc_link_back_start+"<img src=\""+sc_tracking_url+"\" alt=\"StatCounter - Free Web Tracker and Counter\" BORDER=\"0\">"+sc_link_back_end+"</div>");
- }
+else if (window.sc_invisible == 1) {
+ return document.createTextNode("");
}
else {
- document.writeln("<div class=\"statcounter\">"+sc_link_back_start+"<img src=\""+sc_tracking_url+"\" alt=\"StatCounter - Free Web Tracker and Counter\" border=\"0\">"+sc_link_back_end+"</div>");
+ var div_node = document.createElement('div');
+ div_node.className = "statcounter";
+ img_node = document.createElement('img');
+ img_node.setAttribute("src", sc_tracking_url);
+ img_node.setAttribute("alt", "StatCounter - Free Web Tracker and Counter");
+ img_node.setAttribute("border", "0");
+ if (sc_link_back_node == null) {
+ div_node.appendChild(img_node);
+ } else {
+ sc_link_back_node.appendChild(img_node);
+ div_node.appendChild(sc_link_back_node);
+ }
+ return div_node;
}
+
+}
webado
05-20-2005, 05:13 AM
It is not a bug in Statcounter. It is simply not compatible with whatver you are using or else you yourselves don't know how to use it.
Whatever you are outputting to the browser has got to be html. This is where you have to concentrate, to generate html code which includes the Statcounter code at the proper place.
It can be done from PHP, it can be done from ASP. You have to find the way to do it, just like any other html that you output.
If on the other hand your code does not egenrate html (as you see I have no idea what purpose DOM serves), then this is clearly not something where you can use html code with/without javascript, plain and simple.
bediviere
07-06-2005, 06:09 AM
It is not a bug in Statcounter.
Depends on what you want to call a bug. In its "xhtml compliant" code, Statcounter chooses to use a feature of Javascript that is not compatible with XHTML: document.write(). See http://ln.hixie.ch/?start=1091626816&count=1 and http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite, the gist of which say that document.write() cannot be properly supported by any compliant XHTML parser (like those in current browsers). So to say that Statcounter supports XHTML when it doesn't is either a bug in the code or a bug in the documentation.
The XHTML compliant solution is to write to the document not with document.write(), but by using the DOM API (e.g. document.createElement(), document.createTextNode(), node.appendChild(), etc.) to insert nodes in the document. This is what the code I posted does. I made it return the Statcounter DOM node (which you can think of being like returning the Statcounter HTML string), only because at the time, I couldn't think of a standards-compliant way to tell which node in the document contained the Statcounter script. (Thinking about it now, I believe you could just give your SCRIPT node a unique id and find it through document.getElementById("id"), but I'm hesitant to put too much time into thinking about this if no one is interested in providing a truly XHTML-compliant version of Statcounter.)
If you're really completely unfamiliar with how DOM is used in most browsers, please do take a little bit of time to look into it. I guarantee it's worth your time. It's been a W3C Reccomendation since 1998, and practically all modern browsers respect it. (They pretty much have to because Javascript requires it, and they all pretty much support Javascript.)
Also, if you'd like any help with this, I'd be glad to help. I'd love to have a truly XHTML-compliant version of the Statcounter code available. But I understand if no one is willing to put the time into this.
webado
07-06-2005, 08:14 AM
I don't see what the problem is in regards to xhtml proper.
I just tried with a blank page with only statcounter code on it and it validates even as xhtml strict.
See it at http://anysite.webado.net/test1.shtml for xhtml transitional and at http://anysite.webado.net/test2.shtml for xhtml strict.
What more is needed?
Christina, what he is talking about is the "real" xhtml, not pages that are coded as xhtml but served by the server as text/html. The "real" xhtml is served by the server as application/xml+xhtml or similar application, and sutch a page is not supported by IE yet. That's one reason why it's hard to find "real" xhtml sites at this moment, only sites coded as sutch but served as html.
The other reason is that no host (as I know about anyway) have their servers configured to serve "real" xhtml pages, with the xhtml extension. You need to run your own server and know how to configure it right.
If you want to test a "real" xhtml page, look in to this url: http://www.literarymoose.info/=/css.xhtml
With IE you are promted to download the file and when you open it, you see only the code (plain text view). But with Firefox you can see the page just like any other html page.
webado
07-06-2005, 03:51 PM
Arne, I figured it must be something irregular, but the counter is for counting web page views, not other things. Don't know what gave anybody other ideas.
Incidentlaly all I get from that link you posted is this:
Forbidden
You don't have permission to access /=/css.xhtml on this server.
Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request
Arne, I figured it must be something irregular, but the counter is for counting web page views, not other things. Don't know what gave anybody other ideas.
Well, I don't know much about the DHTML/DOM combination, or DOM in whole :) But I understand it as they where about to use the counter on a web site of some kind :lol:
The link to the XHTML page worked when I tested it a couple of minutes ago. So if you like to try again. This time I opened the file in IE and noticed an "improvment" according to IE. You will still get prompted to save or open it. But when you open, you can see a non styled web page (with only the "basic" HTML tags) and not just the code. I'll guess the author has added that to his style sheet and (internal) DTD. But it's a beautiful page in full styles! 8)
webado
07-06-2005, 05:11 PM
Arne, I don't think that /=/ in there is quite right, is it?
But even without that, I have no access to the domain.
So maybe my IP address has been blocked for some reason (hey, I feel insulted :lol: ).
----
hmmm ... I accessed it in FF .... weird - they block IE?????
DISCRIMINATON :lol:
webado
07-06-2005, 05:23 PM
Arne, what the heck is that site about? :lol:
Is it about web design, css and stuff? or is it about art? or is it philosophy?
It's incredibly slow to load and navigate.
(*I feel like such an idiot. :oops: *)
Arne, I don't think that /=/ in there is quite right, is it? But even without that, I have no access to the domain.
So maybe my IP address has been blocked for some reason (hey, I feel insulted :lol: ).
----
hmmm ... I accessed it in FF .... weird - they block IE?????
DISCRIMINATON :lol:
I belive the /=/ thingy is just a unusual way to label the folder (as "=" and "-") :roll: There are "normal" labeled folders also on the site. The server is a bit slow and it takes little time for pages to load, if they load.
Strange if you don't even get the Windows promt window with IE? No problems for me with IE either. Maybe the author has been reading your rantings about W3C, CSS and TOB's? :D He is using and supporting Opera heavely on the site.
bediviere
07-06-2005, 08:20 PM
what he is talking about is the "real" xhtml, not pages that are coded as xhtml but served by the server as text/html. The "real" xhtml is served by the server as application/xml+xhtml or similar application, and sutch a page is not supported by IE yet.
Yeah, this is something like my problem. I have XML pages that are rendered into XHTML using an XSL stylesheet which uses Javascript to generate some dynamic content. If I then include Statcounter's Javascript code, which uses document.write()s, Firefox clears my original content, printing only what was written with document.write(), and IE writes the content written with document.write() over my original content. I believe this is the issue discussed at http://jennifermadden.com/javascript/writeMethod.html, which explains that:
If document.write() is invoked after a page has finished loading, the entire static (non-script generated) content of a page will be replaced with the write method's parameter.
From Microsoft's site:
Syntax
document.write(sText)
Parameters
sText Required. String that specifies the text and HTML tags to write.
Return Value
No return value.
Remarks
You should not use the write or writeln methods on the current document after the document has finished loading unless you first call the open method, which clears the current document's window and erases all variables.
Note When document.write or document.writeln is used in an event handler, document.close should also be used.
Standards Information
This method is defined in World Wide Web Consortium (W3C) Document Object Model (DOM) Level 1.
>> Link to W3C about DOM level 1 (http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/)
>> Why document.write() doesn't work in XML (http://ln.hixie.ch/?start=1091626816&count=1)
>> Solution Title: document.write + application/xhtml+xml + mozilla (http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20483232.html) (You must sign up to see this solution)
vBulletin® v3.8.5, Copyright ©2000-2013, Jelsoft Enterprises Ltd.