webForumDet fria alternativet

ladda in citat med ajax?

JavaScript

2 svar · 616 visningar · startad av vagann

Medlem sedan juli 2011171 inlägg
Frågan#1

hej , jag hade tänkt att ha en citat och då efter några sekunder ska skriptet ladda in en ny citat och så vidare , jag gjorde på detta sätt men det funkade inte ....

test.js

// ==============================================
// Copyright 2004 by CodeLifter.com
// Free for all; but please leave in this header.
// ==============================================

var Quotation=new Array() // do not change this!

// Set up the quotations to be shown, below.
// To add more quotations, continue with the
// pattern, adding to the array.  Remember
// to increment the Quotation[x] index!

Quotation[0] = "Time is of the essence! Comb your hair.";
Quotation[1] = "Sanity is a golden apple with no shoelaces.";
Quotation[2] = "Repent! The end is coming, $9.95 at Amazon.";
Quotation[3] = "Honesty blurts where deception sneezes.";
Quotation[4] = "Pastry satisfies where art is unavailable.";
Quotation[5] = "Delete not, lest you, too, be deleted.";
Quotation[6] = "O! Youth! What a pain in the backside.";
Quotation[7] = "Wishes are like goldfish with propellors.";
Quotation[8] = "Love the river's \"beauty\", but live on a hill.";
Quotation[9] = "Invention is the mother of too many useless toys.";

// ======================================
// Do not change anything below this line
// ======================================
var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}
showQuotation();

sedan i index.html har jag

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script src="test.js" type="text/javascript"></script>
               <div id="twit_r">
                
<script type="text/javascript">// <![CDATA[
$(document).ready(function() {
$.ajaxSetup({ cache: false }); // This part addresses an IE bug.  without it, IE will only load the first number and will never refresh
setInterval(function() {
$('#twit_r').load('/test.js');
}, 3000); // the "3000" here refers to the time to refresh the div.  it is in milliseconds. 
});
// ]]></script>

				<div id="results">Loading...</div>

                </div>

är det nån som kan fixa felet ?tackar ....

Medlem sedan juni 20019 519 inlägg
#2

varför skall du göra det med AJAX? Och om du laddar in många citat, varför skall du ladda in nya räcker ju att du uppdatera whichQuotation och presentera den. Om det nu är så du hämtar ett citat redan ifrån databasen så kan du ju lika väl presentera den direkt i din test.js (test.php?) fil och presentera vad servern ge tillbaka.... annars om det är de 10 citaten du har i din JS fil så ser jag ingen anledning att köra AJAX för att hämta samma arrray om och om igen.

Medlem sedan juli 2004144 inlägg
#3

Det är bättre för SEO att ha content direkt i filen. Mitt tips för kvällen :)

261 ms totalt · 4 externa anrop · v20260731065814-full.1dc6f849
124 ms — deklarationer (db)
0 ms — hämta statistik (cache)
134 ms — hämta tråd, inlägg och bilagor (db)
124 ms — ändringar (db)