ajax post algo
there might not be really a name for this, i just call it this
i made an "algorithm" that replaces {X} in my html file with the table data returned from PHP. i however cant seem to get it to work... i think it should work but im new with RegExp in javascript and i might be doing something wrong im not aware of..
AJAX.jsfunction AJAX(html, php)
{
var header;
var data;
{
var file = php.split('\0');
header = file[0].split('\1');
data = file[1].split('\1');
}
var result = '';
for(var i = 0; i <= data.length - 1; i += header.length)
{
var add = html;
for(var j = 0; j <= header.length - 1; ++j)
{
add.replace(RegExp('{' + header[j] + '}', 'gi'), data[i + j]);
}
result += add;
}
return result;
}test.html<script language='JavaScript' type='text/javascript' src='AJAX.js'></script>
<SCRIPT type="text/javascript">
<!--
var html = '{TEST1} is very {TEST2}';
var php = 'TEST1' + '\1' + 'TEST2' + '\0' + 'CJ' + '\1' + 'SMART';
document.write(AJAX(html, php));
-->
</SCRIPT>
result is {TEST1} is very {TEST2}
[1382 byte] By [
Mitsukai] at [2007-11-20 11:24:06]

# 1 Re: ajax post algo
A few notes:
- In a JavaScript for loop, ++j yields the same result as j++. Use j++ to avoid confusion.
- JavaScript strings are immutable. That is, any functions you run on them return a new string instance instead of altering the string instance you're performing the function on. Therefore, use add = add.replace(...) instead of just add.replace(...) which does nothing.
- i <= data.length - 1 is the same as i < data.length, except for the fact that it's longer.
- In regular expressions, { and } are reserved characters and should be escaped.
- In C-style strings you should always escape backslashes unless they are paired with the character that follows them. Also note that JavaScript does not differentiate between apostrophes and quotes as PHP does ('\r\n' is the same as "\r\n" in JavaScript.)
function AJAX(html, php)
{
var file = php.split("\\0");
var header = file[0].split("\\1");
var data = file[1].split("\\1");
var result = "";
for (var i = 0; i < data.length; i += header.length)
{
var add = html;
for (var j = 0; j < header.length; j++)
{
add = add.replace(new RegExp("\\{" + header[j] + "\\}", "gi"), data[i + j]);
}
result += add;
}
return result;
}
# 2 Re: ajax post algo
thnx man. i know i < length is easier and faster, i already changed it in my code before u posted.
i also know about backslashing because its reserved for {n} (occurrences)
add = add.replace was the thing.. thanks alot
++j is because im a C++ programmer ;)
i use 'string' is because im also a PHP programmer
its just those habits, (i keep them because in PHP and C++ it matters in performance)
also var file was scoped for a reason. also its '\1' not '\\1' its one character not 2
u dont need to tell me how to program things, only need to give the correction in the regexp and the replace function but thanks
# 3 Re: ajax post algo
also var file was scoped for a reason.
You'll find that scopes in JavaScript are not well-defined. Your "scoping" of the file variable does nothing; file is available throughout the whole function.
Also, please don't have a negative attitude because I pointed out the things that were unclear or inefficient in your code (I'm referring to "u dont need to tell me how to program things".) How could I know that you already changed several of the things in your code, or that you had decided to use certain syntax because you like it in other programming languages? If you hadn't been aware of these things my notes might've helped you become aware of them.