Harry Beasant
Harry Beasant

Reputation: 1014

Advert delivery via Javascript document.write

I'm building an advert delivery method and am try to do it through an external Javascript/jQuery page.

I have this so far, but I have some issues with it

$.get('http://url.com/ad.php', {
    f_id: _f_id,
    f_height: _f_height,
    veloxads_width: _f_width
}, function (result) {
    var parts = result.split(",");
    var path = parts[0],
        url = parts[1];
    document.write('<a href="' + url + '" target="blank"><img src="' + path + '"></a>');

I can see the page load, but then after the code above is loaded, it creates a new page with just the advert on it. Is there anyway I can write it onto the page where the code was put?

And this is the script web masters put on their websites to include the adverts:

<script type="text/javascript">

var _f_id = "VA-SQ2TDEXO78N0";

var _f_width = 728;

var _f_height = 90;

</script>
<script type="text/javascript" src="http://website.com/cdn/addelivery.js"></script>

Cheers

Upvotes: 0

Views: 185

Answers (1)

Julien C.
Julien C.

Reputation: 966

is ad.php on the same domain as your script? if it's not have a look at this article

here is a code you could use in your html page, where you want the ad to be inserted:

$.get('http://url.com/ad.php', 
    { f_id : _f_id, f_height : _f_height, veloxads_width : _f_width }
).success(function(result) {
    var parts = result.split(",");
    var path = parts[0], url = parts[1];
    $('body').prepend('<div id="ad_id"><a href="'+url+'" target="blank"><img src="'+path+'"></a></div>');
});

the selector (body here) can be an id, a class, ... (see documentation). You can also use prepend() or html() instead of append, to insert the code where you want ;)

Upvotes: 1

Related Questions