Javascript Error: Undefined or not an object
I am writing a javascript for a Ticker and i am getting some runtime error on the page
When started debugging with VS2008 it displays
Microsfot JS runtime Error: tickerText Undefined
Script as below:
<style type="text/css">
#ticker-area, #ticker-area ul li a {font-size:13px;font-family:Arial, Helvetica, sans-serif;;font-weight:normal;}
</style><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
<style type="text/css">
a:link, a:visited {text-decoration:none}
a:hover {text-decoration:none;}
</style>
<script type="text/javascript">
var tickerText = 0;
function createTicker(){
// put all list elements within #ticker-area into array
var tickerLIs = $("#ticker-area ul").children();
tickerItems = new Array();
tickerLIs.each(function(el) {
tickerItems.push( jQuery(this).html() );
});
i = 0
rotateTicker();
}
function rotateTicker(){
if( i == tickerItems.length ){
i = 0;
}
tickerText = tickerItems[i];
c = 0;
typetext();
setTimeout( "rotateTicker()", 5000 );
i++;
}
var isInTag = false;
function typetext() {
var thisChar = tickerText.substr(c, 1);
if( thisChar == '<' ){ isInTag = true; }
if( thisChar == '>' ){ isInTag = false; }
$('#ticker-area').html(" " + tickerText.substr(0, c++));
if(c < tickerText.length+1)
if( isInTag ){
typetext();
}else{
setTimeout("typetext()", 28);
}
else {
c = 1;
tickerText = "";
}
}
$(document).ready(function() {
var soapEnv =
"<soapenv:Envelop开发者_如何学运维e xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
<soapenv:Body> \
<GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
<listName>Alerts</listName> \
<viewName></viewName> \
<viewFields> \
<ViewFields> \
<FieldRef Name='Title' /> \
<FieldRef Name='Link' /> \
<FieldRef Name='ID' /> \
</ViewFields> \
</viewFields> \
<rowLimit>6</rowLimit> \
</GetListItems> \
</soapenv:Body> \
</soapenv:Envelope>";
$.ajax({
url: "_vti_bin/lists.asmx",
type: "POST",
dataType: "xml",
data: soapEnv,
complete: processResult,
contentType: "text/xml; charset=\"utf-8\""
});
});
function processResult(xData, status) {
$(xData.responseXML).find("z\\:row").each(function() {
var url = "/Lists/Alerts/DispForm.aspx?ID=" + $(this).attr("ows_ID");
var liHtml = "<a href='" + url + "' style='color:#000000;'> "+ $(this).attr("ows_Title") + "...</a>";
$("#listticker").append(liHtml);
});
createTicker();
}
</script>
<div id="ticker-area" style="font-size:13px;overflow:hidden;color: #EEFFFF" width="100%">
<marquee id="listticker" class="hiddenlink" direction="left" scrollamount="4" loop=infinite HSPACE=10 onmouseover="this.stop()" onmouseout="this.start()"></marquee></div>
please help me on this
Thanks in advance
The problem is that tickerText
is local to the rotateTicker
function and does not exist in the typetext
function. A quick way to make this code work is to remove 'var' from line 5, i.e. change
var tickerText = tickerItems[i];
to
tickerText = tickerItems[i];
That will make the tickerText
variable global, just like i
and c
are.
精彩评论