...

View Full Version : JavaScript that refreshes a div



merlin214365
04-18-2009, 01:06 AM
Iím having some problems with a JavaScript that refreshes a div upon clicking a link the problem is that when It passes the file to the JavaScript there are no line breaks, no divs, and there is another java script within that file this script does not work it seems the script filters everything out is there any way around this. Any help would be greatly appreciated.

<script language="javascript">
function loadurl(dest) {
try {
xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) { /* do nothing */ }

xmlhttp.onreadystatechange = triggered;
xmlhttp.open("GET", dest);
xmlhttp.send(null);

}
function triggered() {
if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
document.getElementById("logreport").innerHTML = xmlhttp.responseText;}
}
</script>

The links

<a href="javascript:loadurl('files/logs/test.html')">
<a href="javascript:loadurl('files/logs/test2.html')">

renzaijiang
04-18-2009, 10:47 AM
I think you'd better post the code of test.html'

to execute script code callback you should
split the text wrap by <script> then use function eval(scriptText).

merlin214365
04-18-2009, 03:41 PM
This is what test.html looks like

<html>
<div id="myContent">
</div>
<div id="contentToWrite" style="display:none">
Port: 2<br>
IP: xx.xxx.xxx.xxx Port: 2 Time: 4:50:12 PM Date: 4/8/2009 Data: SYN<br>
IP: xx.xxx.xxx.xxx Port: 2 Time: 4:50:12 PM Date: 4/8/2009 Data: FIN<br>
</div>
<script type="text/javascript">
writeContent(true)
</script>
</html>

do you mean this
document.getElementById("logreport").innerHTML = eval(xmlhttp.responseText);}

merlin214365
04-18-2009, 04:10 PM
Tried alert(xmlhttp.responseText)
And go this response from test.html

<html>
<div id="myContent">
</div>
<div id="contentToWrite" style="display:none">
Port: 2<br>
IP: xx.xxx.xxx.xxx Port: 2 Time: 4:50:12 PM Date: 4/8/2009 Data: SYN<br>
IP: xx.xxx.xxx.xxx Port: 2 Time: 4:50:12 PM Date: 4/8/2009 Data: FIN<br>
</div>
<script type="text/javascript">
writeContent(true)
</script>
</html>

but still no JavaScript activation

merlin214365
04-18-2009, 10:19 PM
Ok so i fixed this by taking

<script type="text/javascript">
writeContent(true)
</script>
</html>

out of test.html and test2.html
And then replacing this line in the java script

document.getElementById("logreport").innerHTML = xmlhttp.responseText;}
with this line
document.getElementById("logreport").innerHTML = xmlhttp.responseText;writeContent(true);}

Now I have a different problem
The script for writeContent is below the problem is that it only works for the first instance of the Div not any other for example if test.html contains this

<html>
<div id="myContent">
</div>
<div id="contentToWrite" style="display:none">
Port: 2<br>
IP: xx.xxx.xxx.xxx Port: 2 Time: 4:50:12 PM Date: 4/8/2009 Data: SYN<br>
</div>
</html>

and test2.html contains this

<html>
<div id="myContent">
</div>
<div id="contentToWrite" style="display:none">
Port: 2<br>
IP: xx.xxx.xxx.xxx Port: 2 Time: 4:50:12 PM Date: 4/8/2009 Data: SYN<br>
</div>
</html>

The script only works for the first html that is opened if i click on the next one it does not work. Any ideas would be greatly appreciated.


<script language="javascript">
var charIndex = -1;
var stringLength = 0;
var inputText;
function writeContent(init){
if(init){
inputText = document.getElementById('contentToWrite').innerHTML;
}
if(charIndex==-1){
charIndex = 0;
stringLength = inputText.length;
}
var initString = document.getElementById('myContent').innerHTML;
initString = initString.replace(/<SPAN.*$/gi,"");

var theChar = inputText.charAt(charIndex);
var nextFourChars = inputText.substr(charIndex,4);
if(nextFourChars=='<BR>' || nextFourChars=='<br>'){
theChar = '<BR>';
charIndex+=3;
}
initString = initString + theChar + "<SPAN id='blink'>_</SPAN>";
document.getElementById('myContent').innerHTML = initString;

charIndex = charIndex/1 +1;
if(charIndex%2==1){
document.getElementById('blink').style.display='none';
}else{
document.getElementById('blink').style.display='inline';
}

if(charIndex<=stringLength){
setTimeout('writeContent(false)',100);
}else{
blinkSpan();
}
}

var currentStyle = 'inline';
function blinkSpan(){
if(currentStyle=='inline'){
currentStyle='none';
}else{
currentStyle='inline';
}
document.getElementById('blink').style.display = currentStyle;
setTimeout('blinkSpan()',500);

}
</script>

renzaijiang
04-19-2009, 10:11 AM
the problem of function setTimeout,
use clearTimeout to stop when click another page

merlin214365
04-20-2009, 02:12 PM
Thank you works great



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum