Javascript replace document.write(ln) with jquery hotfix
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, wh开发者_如何学编程ich 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.
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."
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.
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.
精彩评论