How do I use jQuery to load my advertisements?
Some ads on my website are slowing down loa开发者_JAVA技巧d time. How can I use jquery to load the ad code while the page is loading?
I tried something like this:
$(document).ready(function () {
advertisement();
function advertisement() {
$.ajax({
type: "GET",
url: "/advertisement.php",
data: "type=googleads",
beforeSend: function () {
$("#advertisement").html('Loading ...');
},
cache: false,
success: function (a) {
$("#advertisement").html(a);
}
});
}
});
But it redirects me to the ads company site instead of showing the ads.
Is it possible?
I can see nothing wrong in your code. If you read the Ajax Description of jQuery it defines a attribute called dataType
which is by default: Intelligent Guess (xml, json, script, or html). So by default it should not use text, it should use html. This means that it
Returns HTML as plain text; included script tags are evaluated when inserted in the DOM
if you want to load ad code it is often just JavaScript without need of html and so you should use dataType="script"
which
evaluates the response as JavaScript and returns it as plain text. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true. Note: This will turn POSTs into GETs for remote-domain requests.
As far as you do not give as an example of your advertisement.php
we can not say if your loaded code is correct... I have no experience of Google Ads as your code template seems to load, so if the error is there, sry :)
精彩评论