Title: Cached js doesn&#039;t execute
Last modified: August 30, 2016

---

# Cached js doesn't execute

 *  Resolved [info.brandaware](https://wordpress.org/support/users/infobrandaware/)
 * (@infobrandaware)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/cached-js-doesnt-execute/)
 * I have this simple headlines.js in the body of my front page:
    ` 'keywords = ["
   Headline text one", "Headline text two", "Headline text three", "Headline text
   four" ] var keyword = keywords[Math.floor(Math.random()*keywords.length)] document.
   write(keyword);'  Which is loaded using this code:  '<script type='text/javascript'
   src="<?php echo get_template_directory_uri(); ?>/assets/js/theme/headlines.js"
   ></script>'
 * I’m using Autoptimize in conjunction with W3 Total Cache. Headlines.js runs perfectly
   and updates the headline on every page view only when excluded from Autoptimize.
 * How can I avoid excluding headlines.js and have it work properly?
 * Thanks,
 * Brian
 * [https://wordpress.org/plugins/autoptimize/](https://wordpress.org/plugins/autoptimize/)

Viewing 1 replies (of 1 total)

 *  Plugin Author [Frank Goossens](https://wordpress.org/support/users/futtta/)
 * (@futtta)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/cached-js-doesnt-execute/#post-6541791)
 * morning brian;
    js with document.write cannot be aggregated, as it is supposed
   to write at the point in the HTML where it is located. when aggregated it just
   writes in the void (you’ll probably find it at the very bottom of the page).
 * if you want to be able to aggregate, you’ll have to create an empty
 *     ```
       <div id="randomheadline"></div>
       ```
   
 * and in JS do something like (warning, untested code coming up):
 *     ```
       document.getElementById("randomheadline").innerHTML=keyword;
       ```
   
 * hope this helps,
    frank

Viewing 1 replies (of 1 total)

The topic ‘Cached js doesn't execute’ is closed to new replies.

 * ![](https://ps.w.org/autoptimize/assets/icon-256X256.png?rev=2211608)
 * [Autoptimize](https://wordpress.org/plugins/autoptimize/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/autoptimize/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/autoptimize/)
 * [Active Topics](https://wordpress.org/support/plugin/autoptimize/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/autoptimize/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/autoptimize/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [Frank Goossens](https://wordpress.org/support/users/futtta/)
 * Last activity: [10 years, 8 months ago](https://wordpress.org/support/topic/cached-js-doesnt-execute/#post-6541791)
 * Status: resolved