Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9096641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:54:12+00:00 2026-06-16T23:54:12+00:00

I wrote a website for iPhone/Android build on top of jQueryMobile. This means that

  • 0

I wrote a website for iPhone/Android build on top of jQueryMobile. This means that there are no page refreshes. Instead, all sites are rendered into one page and only the hashtag at the end of the URL changes and causes page transitions.

Now I wanted to embed JavaScript-based ad tags from one of our ad partners. This script code writes the ad (image or js-based ad) via document.write() into the site.

Example:

<script>
 for (var i in adTags){
  var adTag = adTags[i];
  var jscode = adTag.jscode; // Contains document.write() call in <script> Block
  var container = $("#" + adTag.containerId);
  container.html(jscode);
 }
</script>

The problem here is that, while document.write() works well on a static site with page refreshes, a one-page website gets completely destroyed because dynamic calls of document.write() after the HTML document was fully loaded, will empty the <body> tag.

Example:

Assuming my HTML looked like this before:

<html>
 <head>
  <title>Foo</title>
 </head>

<body>
 Hello World
 <div id="ad1"></div>
 <div id="ad2"></div>
</body>

</html>

After running the JavaScript code above, my body looks like this:

<body>
    <img src="http://www.adserver.com/foo.png">
</body>

As you can see, document.write() completely removes all the content of <body>. That’s not tolerable.

So, I do I solve this problem?


Note: I already tried to work around this by overwriting document.write() like this:

<script>
 var currentContainer = null;

 document.write = function(code){
  currentContainer.html(code);
 }

 for (var i in adTags){
  var adTag = adTags[i];
  var jscode = adTag.jscode; // Contains document.write() call in <script> Block
  var container = $(adTag.containerId);
  currentContainer = container;
  eval(jscode);
 }
</script>

Now, while this works well at first sight, the problem here is that the AdTag’s call of document.write() loads another piece of JavaScript which looks like this:

<script src="http://www.adserver.com/more.js"></script>

This js file contains another call of document.write() which would again call my custom (overwritten) document.write() function. The problem now is that in the meantime my for() loop has continued and changed the content of the variable currentContainer and then my custom document.write() function writes the new content into the wrong div container.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T23:54:14+00:00Added an answer on June 16, 2026 at 11:54 pm

    I found that running the ad tag code inside an iframe is the best solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this script to for a contact form on my website, everything works
I am trying to build a website which will display a list of all
I wrote a website that adds information to a Mysql Database. But I want
I have to modify a website that another developer wrote. I need the font
in my website I have some pages structured in this way: www.mysite.com/x/y.php?query=z I wrote
I am working for my college website . I wrote a php script ,that
I want to have a very simple editor for my website that i wrote
I wrote a website a while ago that is a little messy in how
I wrote my own rss-feed for a guestbook from my website so that, with
I wrote a bash script that gets output from a website using curl and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.