Before you tell me this is wrong, bad and/or stupid, let me explain why I'm doing it. Also, I have it working in Firefox. And while I would like some clarifications on the issues at hand, should you have a better solution to the underlying problem, I'm all ears.
I was told to add top banner ads to wordpress blogs. I have no control over when, where, how or why those blogs update their themes. Themes controls the layout of contents and while WP allows you to register callback functions for certain events, start of the body element is no such event. Manually modifying the php code each time a user updates it definitely isn't an option either.
Thus, I concluded that the best way of getting contents at the very top was by using javascript to prepend it to the body element. Next problem is that ads often come as html code containing script and noscript elements. The script elements may contains calls to document.write which is supposed to write new script elements which contains calls to document.write which...
So I set up a singleton which handles replacment of document.write while keeping track of where in the document it's supposed to be "writing", and append new content in the proper place. But making use of the script element only works fully in Firefox, and I also have a couple of questions concerning various other aspects. If I switch to using the noscript elements instead of script elements, the ads show up in FF, Chrome, Safari. So it would seem the issue is only in regards to parsing script element, disregarding IE 8 for now. And I still haven't tested on later IE versions. But I'm mainly interested in what happens in Safari and Chrome for now.
Please note that the code contains the html code for an ad for a pharmacy in sweden, which is what I used when testing. This is the only way I know of reproducing the error (disregarding issues with IE). But I thought you should know that a reference url to the original document will be sent along to the ad server when fetching contents.
First off, the non-working part in Safari and Chrome:
1. Only the first 4 scripts in the script chain are inserted and executed. Are there, or should there be, any differences in how parsing works between appending a script element to a document and having it in the http response to begin with? Or is this a bug? Or is there anything wrong with my code, and in that case, why does it work in Firefox? Any suggestions on how to proceed with debugging? Or should I perhaps post on Safari / Chrome forums instead?
Non-working in IE8
I havn't even started looking at these issues. I mainly include the info so you don't try it out in IE 8. And I havn't event tried running the code in 9 or 10.
- When calling XDomainRequest.get, there is an "Access denied" error.
- When using jQuery for the XHR, there is a "Member not found" (translation from swedish) error in both jQuery 1.9.1 and 1.8.2.
Questions
2. At first I did the XHR without jQuery, since jQuery's docs state that "Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation". But it seems it does allow synchronous cross-domain requests. Should I understand the docs as "cross-domain in conjunction with jsonp", rather then each of them separately?
3. At first I tried to create a new script element "s", set s.src to the url of the script, s.async = false and append it to the document. However, this does NOT execute synchronously. So instead I had to switch to using synchronous XHR to fetch the script contents and assign that to s.text before appending s. Shouldn't execution order of scripts be respected when setting async = false? Else, what's the point of it?
[ATTACH]4881[/ATTACH]
post_ready_document_writer.html.txt