PDA

View Full Version : Is there a code that presents text from a URL source? Document.write maybe?


zachary.etheart
07-02-2008, 11:50 PM
I have a site that uses almost no javascript, but I thought it would be handy for this function. Basically, I have a small DIV box on every page of my site; its position and formatting are kept constant through a "screen" CSS source.

The box's purpose is to display notices about the site such as downtime scheduling and new content notifications. When I don't need it, I simply change the display attribute to "display: none;"

My problem:
But whenever I want to change the message in the DIV, I visit every page and copy and past the message.

I want to know if I can use some JavaScript function to pull a body of text from a single source, like the "screen" attribute of CSS, so that it presents text on each page from one source.

Any feedback is very appreciated, thanks!

rnd me
07-03-2008, 03:30 AM
absolutely. that's a good use of ajax; partial page updates.

let's say you give your div an id, "update";

<div id="update">
<b style="color:red">You must have javascript enabled to view system notices.</b>
</div>

<script>
function el(tid) {return document.getElementById(tid);}

function aGet(turl, callback) { //asynch fetch a url, passes text of file to function callbback's arguments[0]
var XHRt = = !window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
XHRt.onreadystatechange = function () {if (XHRt.readyState == 4 && XHRt.status == 200) {callback(XHRt.responseText);}};
XHRt.open("GET", turl, true);
XHRt.setRequestHeader("Content-Type", "text/html");
XHRt.send("");
return XHRt;
}


function Include(u, dest) {
if (dest && dest.nodeName) {
var ndNam = dest.nodeName.toUpperCase();
switch (ndNam) {
case "TEXTAREA":
case "INPUT":
aGet(u, function (a) {dest.value = a;});
break;
default:
aGet(u, function (a) {dest.innerHTML = a;});
break;
}//end switch
} //end if
}//end function include

</script>




so, if your text to be injected is in a file call "updates.htm", you can add the text into your div container using:

Include("updates.htm", el("update") );