[英]Returning html from javascript
我正在嘗試使用javascript和html在頁面上顯示iframe。 我弄亂了javascript,需要幫助,將html從javascript返回到html。
由於沒有顯示任何內容,因此無法正常工作。 我認為我沒有正確調用該函數。
在我的html中,我有:
<div id="sticky" class="banner-sticky" style="position: fixed;">
</div>
<script type="text/javascript">
var pubReference = 3;
var sessionId = "abcd123";
new Vicinity (pubReference, sessionId, function(result) {
$(function() {
$("#sticky").html(result.ad.getBannerHtml());
displayResults(result);
})
}, function(result) {
$("#sticky").html(result.ad.getBannerHtml());
$(function() {
displayResults(result);
})
});
</script>
在我的JavaScript中,我有:
var Vicinity = function(pubReference, sessionId, done, err) {
// defaults
this.pubReference = pubReference;
this.ad = {
getBannerHtml: function() {
return '<iframe src="http://ad.vic.co/banner?pr='+this.pubReference +'&lat'+ this.lat+'=&lon=' + this.lon + '"></iframe>'
}
};
this.getLatLng = function(vicinity, done, err) {
if (geoPosition.init()) { // Geolocation Initialisation
geoPosition.getCurrentPosition(done, function(e) {
console.warn('ERROR: ' + e.message);
err(vicinity);
}, { enableHighAccuracy:true } );
} else {
console.warn("Browser doesn't support Geolocation");
err(vicinity);
}
};
this.init = function(done, err) {
var that = this;
this.getLatLng(that, function(position) {
that.lat = position.coords.latitude;
that.lng = position.coords.longitude;
});
};
this.init(done, err);
};
我認為這就是您要尋找的東西?
HTML:
<div id="sticky" class="banner-sticky" style="position: fixed;">
Test
</div>
JS:
var Vicinity = function (pubReference, sessionId, done, err) {
// defaults
this.sessionId = sessionId;
this.pubReference = pubReference;
this.finishedGettingAd = false;
this.ad = {
getBannerHtml: function() {
return '<iframe src="http://www.lifehacker.com"></iframe>';
}
};
this.getLatLng = function(vicinity, done, err) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(done, function(e) {
console.warn('ERROR: ' + e.message);
err(vicinity);
}, { enableHighAccuracy:true } );
} else {
console.warn("Browser doesn't support Geolocation");
err(vicinity);
}
};
this.init = function(done, err) {
var that = this;
this.getLatLng(that, function(position) {
that.lat = position.coords.latitude;
that.lng = position.coords.longitude;
}, err);
if(typeof done === 'function')
done(this);
};
this.init(done, err);
};
$(document).ready(function() {
var pubReference = 34;
var sessionId = "abcd123";
var data = new Vicinity (pubReference, sessionId,
function(result) {
$("#sticky").html(result.ad.getBannerHtml());
//displayResults(result);
}, function(result) {
$("#sticky").html(data.ad.getBannerHtml());
//displayResults(result);
}
);
});
請注意,Vicinity實例的初始化已在文檔內部准備就緒(因此您可以在完成/錯誤之后適當地修改DOM)。 另外,您沒有將err傳遞給getLatLng。 我還在初始化的末尾插入了對done()的函數調用(我假設這就是您想要的)。 如果沒有,我的小提琴將其刪除:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.