User login |
What I have learned so far Ajax-ing with prototype library
After 3 mounths (at the time updating this it is longer) ajaxing with prototype library and javascript a little more that usual, Cross domain javascripts
Cant have cross domain calls. Ajax javascripts cant GET and IE cache
IE has an amazing cache mechanism! For example I was making an Ajax call Clear select box on IE
A typical case using ajax is to populate a select box. The non typical is that
while( oStates.options.length>0) {
oSelect.remove(0);
}
Amazinly when last option is removed select box is also removed from the page!!
for( i=oStates.options.length-1; i>=0; i--) {
oSelect.options[i]=null;
}
Traversing XMLNote that I am a common guy, just a php coder, not a Javascript-XML expert.
Back to the usual example. When you want to populate a select box you make a call to
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<response>
<record>
<id>1</id>
<item>Item 1</item>
</record>
<record>
<id>2</id>
<item>Item 2</item>
</record>
<record>
<id>3</id>
<item>Item 3</item>
</record>
</response>
PHP part to make that is simple. Now its javascript time. We get server's response in XML
var tagRecords = originalRequest.responseXML.getElementsByTagName("record");
Returns a collection of all 'record' elements in our XML. Now we need a loop to
for(i=0; i<tagRecords.length; i++) {
var record = tagRecords[i];
var sValue = record.getElementsByTagName("id")[0].textContent;
var sText = record.getElementsByTagName("item")[0].textContent;
}
OK? Firefox is ok with that but IE not! Get a javascript error? No.
for(i=0; i<tagRecords.length; i++) {
var record = tagRecords[i];
var sValue = record.getElementsByTagName("id")[0].firstChild.nodeValue;
var sText = record.getElementsByTagName("item")[0].firstChild.nodeValue;
}
Have to .firstChild.nodeValue not .textContent. How about empty XML tags?
On one case I used XML to return info after doing an Ajax record addition to a db table.
print '<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>';
print '<response>';
print '<msg>'.$msg.'</msg>';
print '<success>'.$success.'</success>';
print '</response>';
If operation fails $success=0 and $msg has mysql_error() message, otherwise $success=1 and
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<response>
<msg></msg>
<success>1</success>
</response>
Note the empty msg tag. Will previous paragraph's javascript work? No. Again the mysterious Now i have found something better. Lets get in hands the traversing code we saw earlier:
for(i=0; i<tagRecords.length; i++) {
var record = tagRecords[i];
var sValue = record.getElementsByTagName("id")[0].firstChild.nodeValue;
var sText = record.getElementsByTagName("item")[0].firstChild.nodeValue;
}
This loop fails if id tag is empty, because record.getElementsByTagName("id")[0] has no childs, so all we have to do, is add a check if record.getElementsByTagName("id")[0] has any childs. In case of empty tag it doesnt have! So adding an if statement will fix the problem:
for(i=0; i<tagRecords.length; i++) {
var record = tagRecords[i];
var sValue;
if( record.getElementsByTagName("id")[0].firstChild==null )
sValue = '';
else
sValue = record.getElementsByTagName("id")[0].firstChild.nodeValue;
var sText = record.getElementsByTagName("item")[0].firstChild.nodeValue;
}
Same thing should be done for sText also. But thats the idea. NOWRAP attribute of a TD
Passing html throu Ajax (xml) nowrap attribute is converted to nowrap=""!! Ajax updater returns Javascript along with HTML
Now the case that Susana put up is what if ajax call returns javascript along with HTML.
var myAjax = new Ajax.Updater(
'container',
url,
{
asynchronous:true,
method: 'get',
parameters: pars,
evalScripts: true,
onFailure: reportError
}
);
But there is also one more point that needs to take care.
<script language="javascript" type="text/javascript">
function sayHi(){
alert('Hi');
}
</script>
<input type=button value="Click Me" onclick="sayHi()">
<script language=\"javascript\" type=\"text/javascript\">
sayHi = function(){
alert('Hi');
};
</script>
See a working demo.
But what I prefer to do? Load all javascript code to the initial page (the one with the container div).
function action_confirm(msg, recid) {
if( confirm(msg) )
....
}
And on record specific delete link something like onclick="action_confirm('Delete row?,343);". Last update on 30 Oct 2008. |