Forum Moderators: open

Message Too Old, No Replies

document.write & "if" condition

document.write & "if" condition AJAX

         

Minuteman

9:51 pm on Nov 17, 2009 (gmt 0)

10+ Year Member



I'm sort of a newbie at javascript so little things can easily trip me up. I'm being stonewalled by not being able to "echo" the values when and in between the php page and the javascript functions. The below debug code will so far write the text part of the sesscheck variable but then the document.getElementById stops functioning.(i.e. the page will refresh but instead of showing the target page it shows a blank page with the text part of the debug document.write command).

function handleResponse() {
if(http.readyState == 4) {
var response = http.responseText;
var sesscheck = "<?php echo $_SESSION['page_num']; ?>";
//if (sesscheck == NULL){
document.write('sesscheck = ', sesscheck);
//}
document.getElementById('foo').innerHTML = response;
}
}
var sndreqtimer = 10000;

setInterval('sndReq()', sndreqtimer);


QUESTION #1...Am I correct in believing that setInterval is still working ok because the document.write('sesscheck gets written?

QUESTION #2 ... If so, then why does the document.getElementById method right after it stop working?

QUESTION #3 ... My need is to get the "if" conditional (just before the document.write) working but uncommenting it seems to cause the setInterval to not work at all (and never call the sndReq function). The initial page just sits there and never loads the target page nor displays the document.write as before. Why does uncommenting the "if" part of the code break everything else?

daveVk

2:37 am on Nov 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do not use document.write for debug, cause of #2, consider using alert() or add to innerHTML.

var sesscheck = "<?php echo $_SESSION['page_num']; ?>";
results in sesscheck containing that string, php can not execute in the clients browser.

Consider doing something like this on php side
echo '<input type="hidden" id="idPageNo" value="' . $_SESSION['page_num']. '">';

use document.getElementById("idPageNo").value in js

Fotiman

2:08 pm on Nov 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



document.write, if called after the page has finished loading, will OVERWRITE your existing document. In your case, you're making an AJAX request, which completes after the page has loaded, and the call to document.write will overwrite the existing page.

document.write should be avoided. Instead, use innerHTML or DOM methods to append to the DOM.