Примеры динамического добавления скриптов
Загрузка скрипта с использованием AJAX
Тест 1 : кешируется
<a href="javascript:test1()">Тест 1 : кешируется</a>
<div id="status-test-1"></div>
<script>
function test1(){
id('status-test-1').innerHTML = 'Начинаем загрузку скрипта...';
SRAX.addScript({url:'addscript/test1.js', callback:function(){
id('status-test-1').innerHTML = 'успешно законченно';
}})
}
</script>
Тест 2 : не кешируется
<a href="javascript:test2()">Тест 2 : не кешируется</a>
<div id="status-test-2"></div>
<script>
function test2(){
id('status-test-2').innerHTML = 'Начинаем загрузку скрипта...';
SRAX.addScript({url: 'addscript/test2.js', callback: function(){
id('status-test-2').innerHTML = 'успешно законченно';
}, nocache: true})
}
</script>
Загрузка скрипта стандартным способом (в основном используется для крос-доменной загрузки скриптов)
Тест 3
<a href="javascript:test3()">Тест 3</a>
<div id="status-test-3"></div>
<script>
function test3(){
id('status-test-3').innerHTML = 'Начинаем загрузку скрипта...';
SRAX.addScript({url: 'addscript/test3.js', callback: function(){
id('status-test-3').innerHTML = 'успешно законченно';
}, noax: true})
}
</script>
Примеры динамической загрузки сложных скриптов (Google Adsense и Google Map)
Google Adsense
<a href="javascript:GoogleAdsense()">Google Adsense</a>
<div id="GoogleAdsense"></div>
<br/>
<script>
function GoogleAdsense(){
id('GoogleAdsense').innerHTML = 'Начинаем загрузку скрипта...';
SRAX.addScript({url: 'http://pagead2.googlesyndication.com/pagead/show_ads.js', callback: function(){
id('GoogleAdsense').innerHTML = 'успешно законченно';
}, noax: true})
}
</script>
Google Map
<a href="javascript:GoogleMap()">Google Map</a>
<div id="GoogleMap"></div>
<br/>
<div id="map-place"></div>
<script>
function GoogleMap(){
id('GoogleMap').innerHTML = 'Начинаем загрузку скрипта...';
SRAX.addScript({
url: 'http://maps.google.com/maps?file=api&v=2&key=YOUR-GOOGLE-MAP-KEY',
callback: function(){
id('GoogleMap').innerHTML = 'успешно законченно';
var div = id('map-place').appendChild(document.createElement('div'));
div.style.height = '300px';
div.style.width = '600px';
div.style.border = '1px solid #000';
div.style.marginBottom = '30px';
var map = new GMap2(div);
map.setCenter(new GLatLng(50.4522, 30.2968), 8);
},
noax: true
})
}
</script>