views:

307

answers:

3

A seriously flawed and retarded piece of software which goes by the name of "Joomla" is giving me its usual load of headaches.

Sample code

I have the following code:

....
<div id="abc"><!----></div>
<script type="text/javascript">
    jQuery.get(url,function(data){
        jQuery('#abc').html(data);
    },'data');
</script>
....

And I get this code from that url:

....
<script type="text/javascript">
   document.write('<span');
   // perhaps write classes
   document.write('>');
   // and the rest of the code
</script>
....

The Issue

Joomla is being modern by using document.write snipets. This completely obliterates any AJAX html, unless I disable/strip out javascript, which is a huge NO.

The Fix

I need to replace the text/code progressively to look like:

<span id="ob1"><!----></span>
<script type="text/javascript">
   _ob_begin(1);
   _ob_write(1,'<span');
   // perhaps write classes
   _ob_write(1,'>');
   // and the rest of the code
   _ob_end(1);
</script>

Clarification

Joomla-lovers, don't get anywhere near this topic. I feel like burning a joomla dev alive right now.

The generated code (document.write) is strictly joomla's doing...no plugins, components or anything.

+5  A: 

I personally would not use Joomla, if I had to I'd probably go seek in the code to find that piece of code and destroy it with my laser gun.

But, a quick 'n' dirty fix might be output buffering in PHP, you can use this to catch the entire contents of a page, do your magic in there, and then output it to the browser. So in your case, you could use it to find/replace/remove the document.write's.

Do I advise this approach? No, but if you truely can't touch the generated code (and I wouldn't know, so trusting your judgement), you don't have many options left.

CharlesLeaf
Hi there! I prefer Wordpress over joomla. Then again, I would prefer my own framework a million times more! But heh, you know clients. I can't do any output buffering - I'm reading a joomla article via ajax.
Christian Sciberras
I know clients indeed, so that's why I offered a plausible solution instead of just talking smack about Joomla (how much I enjoy it tho) ;) Oh but do you control the joomla article page (output buffer on that page?) or are you looking for a replacement in javascript?
CharlesLeaf
Hey, I really can't be blamed. I've got a rant-essay on my home pc, some 1k words all about joomla, which I never got published... Do you know how I could do the replacements as I suggested in my fix?
Christian Sciberras
If you want to do the "transformation" in JavaScript you'd probably do it before writing it away. Seeing how the information is in `data` you'd probably want to search the text in `data` for document.write and replace it with your own methods.. shouldn't be too difficult?
CharlesLeaf
1. String.replace only replaces one item. 2. Since there may be different <script> blocks, I need to replace them individually, ie, so that each script block writes in a different unique span.
Christian Sciberras
Normally I'd try and make a small test-case for you, however right now I'm kind of stuck in my own work.. Maybe you could create a vanilla (so outside of joomla) PHP file that requests the file and then still do the transformations in PHP, and with Ajax you just call that PHP file.. Also @bobince (the other answer) makes a very valid point..
CharlesLeaf
I think I'll be trying a totally different route altogether...a fix to this is getting infeasible by the second.
Christian Sciberras
+7  A: 

It is possible to overwrite document.write with your own function that buffers output text and after a delay writes it to some element's innerHTML. This isn't wholly reliable but it can catch many common cases.

However, you can't load <script> content by writing it with innerHTML/html()). jQuery's load() attempts to work around some of the problems, but it's fundamentally broken, even before the problems with document.write (which is indeed filthy).

You could try to write the content to innerHTML, pick out the script elements, replace them with empty divs, and eval() their text (this is kind of what load() tries to do, not really successfully), with document.write overridden to throw completed output to the div. But it's going to be messy and unreliable. I don't know what your use case is, but there is almost certainly a better approach than this.

bobince
That's what I'm trying to do, but as you correctly note, overwriting document.write/ln doesn't really help, since I don't know *where* to put the content. So I need to at least, replace progessively, ie, preserve each individual item's id.
Christian Sciberras
+12  A: 

One way would be to do a find and replace using Programmers Notepad 2 on all files in that folder that end in .js. This might help accomplish "unless I disable/strip out javascript, which is a huge NO."

Jimmie Clark