0

My sample php $advert variable as follows;

<script src="http://ad.example.com/scripts/rp.js" type="text/javascript"></script><script src="http://ad.example.com/scripts/rp.js" type="text/javascript"></script>
<script type="text/javascript">
    document.write("<"+"script src='http://ad.example.com/rpgetad.ashx?tt=t_&ciid=9775202&rnd="+Math.random()%99999999+"'></"+"script>");
</script>

I want to create js variable with it.

var advert = "<?php echo rawurlencode($advert); ?>";

then i want to decode and append it to specified div and execute it.

var content = decodeURIComponent(advert);
document.getElementById("mydiv").innerHTML = content;

But when i try it doesn't work.

I also tried php's json_encode() and $('<div/>').html(contents).text() decode methods.

I need to do this with javascript if it is posible.

I am caching html responses. Advertising codes from partners are changing every day. So i dont want to destroy page's cache every time. I am generating seperated js file with php to do that.

3
  • 2
    Why don't you just output the html directly in the php script, why go through the trouble of first putting it into javascript then adding it? Commented Dec 29, 2015 at 21:15
  • I am caching responses in redis. Advertising codes are randomly generated and i dont want to destroy cache every time. Commented Dec 29, 2015 at 21:21
  • 1
    Scripts aren't executed when you assign to .innerHTML. You need to use document.createElement("script"); Commented Dec 29, 2015 at 22:01

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.