ซื้อขายบ้านมือสอง

วันพุธที่ 30 เมษายน พ.ศ. 2551

javascript สำหรับ random banner มาแสดง แบบง่ายๆ

บทความนี้ พอดีเพื่อนผม เค้าอยากจะ ได้ script random banner มาแสดง แบบง่ายๆ นะครับ ซึ่งผม ก็เลือกใช้ javascript ในการแสดงผล random นะครับ
script นั้นก็มีเพียงสั้นๆ ดังนี้นะครับ

<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<script language="javascript">
// ฟังชั่น random
function get_random()
{
var ranNum= Math.floor(Math.random()*5);
return ranNum;
}
// ฟังชั่น จัด queue
function getQueue(frameId)
{
var frame = document.getElementById(frameId);
var whichQuote=get_random();
var quote=new Array(5)
quote[0]="http://www.sanook.com/"; // ไฟล์ แบนเนอร์ ที่ 1
quote[1]="http://www.kapook.com/"; // ไฟล์ แบนเนอร์ ที่ 2
quote[2]="http://www.thaieasy2home.com/"; // ไฟล์ แบนเนอร์ ที่ 3
quote[3]="http://www.google.com/"; // ไฟล์ แบนเนอร์ ที่ 4
quote[4]="http://www.yahoo.com/"; // ไฟล์ แบนเนอร์ ที่ 5

frame.src=quote[whichQuote]; // แสดงผล Iframe
}
// ปรับขนาด Iframe
function autoIframe(frameId){
try{
frame = document.getElementById(frameId);
innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;
objToResize = (frame.style) ? frame.style : frame;
objToResize.height = innerDoc.body.scrollHeight + 30;
}
catch(err){
window.status = err.message;
}
}
</script>
</HEAD>


<BODY>
<iframe id="frame" name="frame" frameborder="0" scrolling="no" height="60" width="468" marginwidth="0" marginheight="0" onload="getQueue('frame');autoIframe('frame');"></iframe>
</BODY>
</HTML>


หากต้องการเพิ่ม ให้ script สามารถ ตั้งเวลาในการ random เองอัตโนมัติ ก็เพียงแค่ เพิ่ม setTimeout ลงไปนะครับ

<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<script language="javascript">
// ฟังชั่น reload มี parameter 2 ตัวคือ t=วินาที , u = URL
function reload_view(t,frame) {

setTimeout("getQueue('frame');",(t * 1000));
}

// ฟังชั่น random
function get_random()
{
var ranNum= Math.floor(Math.random()*5);
return ranNum;
}


// ฟังชั่น จัด queue
function getQueue(frameId)
{
var frame = document.getElementById(frameId);
var whichQuote=get_random();
var quote=new Array(5) // ใส่จำนวน ของ banner ที่ต้องการนำมา random
quote[0]="http://www.sanook.com"; // ไฟล์ แบนเนอร์ ที่ 1
quote[1]="http://www.kapook.com"; // ไฟล์ แบนเนอร์ ที่ 2
quote[2]="http://www.thaieasy2home.com"; // ไฟล์ แบนเนอร์ ที่ 3
quote[3]="http://www.google.com"; // ไฟล์ แบนเนอร์ ที่ 4
quote[4]="http://www.yahoo.com"; // ไฟล์ แบนเนอร์ ที่ 5

frame.src=quote[whichQuote]; // แสดงผล Iframe
}
// ปรับขนาด Iframe
function autoIframe(frameId){
try{
frame = document.getElementById(frameId);
innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;
objToResize = (frame.style) ? frame.style : frame;
objToResize.height = innerDoc.body.scrollHeight + 30;
}
catch(err){
window.status = err.message;
}
}
</script>
</HEAD>


<BODY>
<iframe id="frame" name="frame" frameborder="0"
scrolling="no" height="60" width="468" marginwidth="0" marginheight="0"
onload="reload_view(10,'frame');autoIframe('frame');"></iframe>
</BODY>
</HTML>