On Dec 16, 2012, at 8:59 AM, tab1ta wrote:

> Hello.
> I'm studying, there is a piece of code i cannot get to work, so i was asking 
> myself if you can help, given that all the pastebin doesn't accept the 
> prototype link.

Which ones have you tried? JSFiddle has Prototype as one of its many libraries, 
ready to use. This has the added benefit that the code is rendered and runs in 
your browser while you are working on your example. Others can fork your 
"fiddle" and fix it there, too.

> I'm not exaclty sure of what it does, i assume it goes through the document 
> dom to place every div page in the right order to be viewed. Its a php file, 
> and i can't really understand why php is needed to solve this task, however 
> is a pice of code found in a book, it could be wrong.
> 
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
> "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd";>
>     <html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en">
>         <head>
>             <script type="text/javascript" src="prototype.js"></script>
>             <script type="text/javascript"> 
>             function importNode(p_element, p_allChildren) {
>                 switch (p_element.nodeType) {
>                     case 1:
>                         var newNode = 
> document.createElement(p_element.nodeName);
>                         if (p_element.attributes && 
> p_element.attributes.length >0)
>                             for (var i = 0, il=p_element.attributes.length; i 
> < il;)
>                                 
> newNode.setAttribute(p_element.attributes[i].nodeName,
>                                         p_element.getAttribute(
>                                         p_element.attributes[i++].nodeName));
>                         if(p_allChildren && p_element.childNotes && 
> p_element.childNodes.length > 0)
>                             for (var i = 0, il = p_element.childNodes.length; 
> i < il;)
>                                 
> newNode.appendChild(importNode(p_element.childNodes[i++], p_allChildren));
>                         return NewNode;
>                         break;
>                     case 2:
>                     case 3:
>                         return document.createTextNode(p_element.nodeValue);
>                         break;
>                 }
>             };

No Prototype here, could be written much more clearly and concisely with 
Prototype idioms.

>             
>             function turnPage(p_number) {
>                 var pages = $('article').getElemenetsByTagName('div');

TYPO??? Elemenets !== Elements

(also could be written as $('article').select('div') for added points)

>                 for (var i = 0, il = pages.length; i< il; i++) {
>                     $(pages[i]).setStyle({display: 'none'});
>                     $('l' + (i+1)).innerHTML = '<a href="article.php?page=' + 
> (i+1)+ '"onClick="return turnPage('+ (i+1)+')">' +(i+1)+'</a>';
>                 }
>                 if ($('page' + p_number).innerHTML == '') {
>                         new Ajax.Request('article.php', {
>                             method: 'post', 
>                             parameters: {page:p_number }, 
>                             onSuccess: function(xhrResponse) {
>                                 var response = xhrResponse.responseXML;
>                                 var newNode;
>                                 if (!window.ActiveXObject) {
>                                     newNode =
>                                         
> document.importNode(response.getElementsByTagName('page')[0].childNodes[1], 
> true);
>                                     $('page' + p_number).appendChild(newNode);
>                                 } else {
>                                     newNode = 
>                                         
> importNode(response.getElementsByTagName('page')[0].childNodes[0], true);
>                                     $('page' + p_number).appendChild(newNode);
>                                 }
>                                 $('l' + p_number).innerHTML = p_number;
>                                 $('page' + p_number).setStyle({display: 
> 'block' });
>                             },
>                             onFailure: function(xhrResponse) {
>                                 $('page').innerHTML = xhrResponse.statusText;
>                             }
>                         });
>                     } else {
>                         $('l' + p_number).innerHTML = p_number;
>                         $('page'+ p_number).setStyle({display: 'block' });
>                     }
>                     return (false); 
>                 }</script>

The rest of this is a salad of Prototype and non-Prototype coding styles. 
There's nothing wrong with a for loop, until you try to loop over a collection 
of Prototype-extended elements. That's when you meet the "salt" as opposed to 
the "syntactic sugar" of Prototype's each() method.

If you can write out, in English as opposed to JavaScript, what it is you're 
trying to accomplish here with this code, I might be able to point you toward 
another approach that you might find more readable/understandable.

Walter

>             <title>Paged navigation</title>
>             </head><body>
>             <h1> Paged Navigation </h1>
>             <div id="article">
>                 <div id="page1">
>                     <p>This is page one.</p>
>                 </div>
>                 <div id="page2">
>                     <p>This is page two.</p>
>                 </div>
>                 <div id="page3">
>                     <p>This is page three.</p>
>                 </div>
>                 <div id="page4">
>                     <p>This is page four.</p>
>                 </div>
>                 <div id="page5">
>                     <p>This is page five.</p>
>                 </div>
>             </div>
>             <div id="pageNavContainer">
>                 <ul id="pageNavList">
>                     <li id="l1">1</li>
>                     <li id="l2">
>                         <a href="article.php?page=2" onClick="return 
> turnPage(2);">2</a>
>                     </li>
>                     <li id="l3">
>                         <a href="article.php?page=3" onClick="return 
> turnPage(3);">3</a>
>                     </li>
>                     <li id="l4">
>                         <a href="article.php?page=4" onClick="return 
> turnPage(4);">4</a>
>                     </li>
>                     <li id="l5">
>                         <a href="article.php?page=5" onClick="return 
> turnPage(5);">5</a>
>                     </li>
>                 </ul>
>             </div>
> </body> </html>
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Prototype & script.aculo.us" group.
> To view this discussion on the web visit 
> https://groups.google.com/d/msg/prototype-scriptaculous/-/Ibtlp8ZSnzAJ.
> To post to this group, send email to prototype-scriptaculous@googlegroups.com.
> To unsubscribe from this group, send email to 
> prototype-scriptaculous+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/prototype-scriptaculous?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.

Reply via email to