最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript replace document.write(ln) with jquery hotfix - Stack Overflow

programmeradmin2浏览0评论

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 pletely 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, ponents or anything.

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 pletely 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, ponents or anything.

Share Improve this question edited Jun 8, 2012 at 14:18 Christian asked Sep 30, 2010 at 9:35 ChristianChristian 28.1k17 gold badges116 silver badges159 bronze badges 7
  • 5 and your problem is your headaches?? – Reigel Gallarde Commented Sep 30, 2010 at 9:37
  • No. Joomla code being old, inconsistent and damn stupid. – Christian Commented Sep 30, 2010 at 9:40
  • I need to do the replacement I mentioned above to get it working as desired. JS' String.replace() has several limitations, and I'm not sure what's the best way to do what I want (replacements). – Christian Commented Sep 30, 2010 at 9:53
  • I must admit I'm very amused with people's entirely expected behaviour regarding the undeniable truth of joomla's inferiority - to which they resort with throwing stones at the topic ;-). – Christian Commented Sep 30, 2010 at 10:59
  • 2 If you are a developer dedicated to your client, you don't rant about how awful the framework your client want, but instead, suggesting him a better framework. – rob waminal Commented Oct 1, 2010 at 1:58
 |  Show 2 more ments

3 Answers 3

Reset to default 12 +50

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 acplish "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 mon 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 pleted 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.

发布评论

评论列表(0)

  1. 暂无评论