0

i have a website where news articels are shown.

now i already implemented a ajax reload for the articel so i can switch from one to another without reloading the whole page. (Like in News Apps)

there is a box with advertisment in every articel aswell. This box is filled with an ad which is placed there with document.write. (OpenX)

When i now reaload an articel the content of the box cant be written there because my document is already closed and the new articel is only loaded there with jQuery and AJAX. So when i reaload the articel the content in the document.write cant be written into the box and is instead just placed unter my webseite, which looks likes some kind of error code.

Now my Question is. How can i get the Content into the Box? Is there a away to open a closed document to write it there or do i need to replace all document.write lines with, for example, jQuery append();?

I've already tried to replace some of the parts with jQuery append(); But there i get some problems too, like this one is working fine:

document.write ("<td style='width:100px;'>"); //old
jQuery("#werb1").append("<td style='width:100px;'>"); //new 

But this one doesnt work at all:

document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u); //old
jQuery("#werb1").append("<scr"+"ipt type='text/javascript' src='"+m3_u) //new

m3_u is a URL saved in the varible like https://ad.domain.openx/www/delivery/ajs.php

Ferdinand Fatal
  • 374
  • 3
  • 16
  • 1
    You are not closing the `script` tag. `"` – kosmos Jul 22 '15 at 07:04
  • Are you seriously appending via jQuery another javascript script?? – Lelio Faieta Jul 22 '15 at 07:06
  • m3_u is a URL saved in the varible like https://ad.domain.openx/www/delivery/ajs.php. the tag isnt closed because there are many more lines with document.wirte and is finaly closed after 10 different document.write with document.write ("'><\/scr"+"ipt>"); – Ferdinand Fatal Jul 22 '15 at 07:06

2 Answers2

1

You cannot pass to already rendered table. You need to rerender whole table and close script tag

If you are using dynamic content on website you may condier of using ANGULARJS or ReactJS or Meteor

Here are full sets of tools wich would really help you with building such webapp.

But atm you need to remove table, build whole HTML in your variables and put it on website. Browsers, even if you would put that TD correctly, will push td or tr outside of table.

Paweł Smołka
  • 638
  • 5
  • 13
  • what do you mean with rerender? is there any script line i can use for that or do you mean something like a reload of the webseite? – Ferdinand Fatal Jul 22 '15 at 07:08
  • I do not know such script. I was doing such thing with angular so it was working easly (it does iterate and rework table) but as I remember I had problem wiht jQuery append. So what I have done was to just create new table with new fields, drop old and put new in its container. – Paweł Smołka Jul 22 '15 at 07:11
1

The JavaScript document.write is only executed on page load. So if you're using Ajax, you cannot use document.write after a document has loaded, or it'll override all the existing content on the page.

Using Ajax with document.write is initially a bad idea, as the ads won't get reloaded on Ajax requests.

The best approach would be use Ajax instead of document.write if possible for the ads. As you already have working Ajax code, it shouldn't be hard to implement the same functionality for the ads as well.

But if that's not an option, then you can use jQuery append(). For example, if your old code looks like this:

<div>
  <script>
    document.write("<h1> Heading </h1>");
  </script>
</div>

Then you can replace with:

$("div").append("<h1> Heading </h1>");

Both of them would produce the same result:

<div>
  <h1> Heading </h1>
</div>

Lastly, if you know server-side scripting, then you can use that with Ajax and create interactive modern websites.

Abraar Arique Diganto
  • 1,215
  • 16
  • 24
  • Thank you for that advice. I will try wo workaround this. maybe there is a way to change the adserver code which is given with document.write – Ferdinand Fatal Jul 22 '15 at 08:59