Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Beginner problem about arrays in while loop (driving me crazy!)

    Hey,

    This has been driving me crazy because it seems so straightforward. Here is my code, and then I'll explain what the problem is:

    Code:
    var uiWebview_SearchResultCount = 0;
    var searchPositions = new Array();
    
    function uiWebview_HighlightAllOccurencesOfStringForElement(element,keyword) {
        searchPositions = new Array();
        
        if (element) {
            if (element.nodeType == 3) {        // Text node
                while (true) {
                    //if (counter < 1) {
                    var value = element.nodeValue;  // Search for keyword in text node
                    var idx = value.toLowerCase().indexOf(keyword);
                    
                    if (idx < 0) break;             // not found, abort
                    
                    //(value.split);
                    
                    //we create a SPAN element for every parts of matched keywords
                    var span = document.createElement("span");
                    var text = document.createTextNode(value.substr(idx,keyword.length));
                    span.appendChild(text);
                    
                    span.setAttribute("class","uiWebviewHighlight");
                    span.style.backgroundColor="black";
                    span.style.color="white";
                    
                    uiWebview_SearchResultCount++;    // update the counter
                    
                    text = document.createTextNode(value.substr(idx+keyword.length));
                    element.deleteData(idx, value.length - idx);
                    var next = element.nextSibling;
                    element.parentNode.insertBefore(span, next);
                    element.parentNode.insertBefore(text, next);
                    element = text;
                    
                    searchPositions[uiWebview_SearchResultCount-1]=span.offsetTop - 140;
                    window.scrollTo(0, searchPositions[uiWebview_SearchResultCount-1]);
                }
            } else if (element.nodeType == 1) { // Element node
                if (element.style.display != "none" && element.nodeName.toLowerCase() != 'select') {
                    for (var i=element.childNodes.length-1; i>=0; i--) {
                        uiWebview_HighlightAllOccurencesOfStringForElement(element.childNodes[i],keyword);
                    }
                }
            }
        }
        
        currentSearchScroll=-1;
    }
    
    function scrollToNextSearch(){
        currentSearchScroll++;
    //    testVariable=currentSearchScroll;
        window.scrollTo(0, searchPositions[uiWebview_SearchResultCount-1-currentSearchScroll]);
    }
    Basically, I am iterating through a document and storing the positions of search terms as they are found into the searchPositions. I then scroll to the latest found search function (bottom to top), so I scroll to the first found position (this part works). The idea is to call scrollToNextSearch() to iterate through the search functions. However, the array is "empty" in this function call (I tested our searchPositions.length() and it was 0. It is not a problem with the global variables, as I tested setting fake values outside of the while loop. The problem is that the values set in the while loop are not actually set. Is this a closure problem? How can I fix this? Thanks!

  • #2
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Hi,

    Before I even try to analyze and debug the code, three questions:

    1) essentially, this function is part of a page (either on the page or in a js file)?
    2) why are you re-assigning searchPositions = new Array();
    on the first line in the function?
    3) provide me with an example of your parameters

    or...a page where this is running
    Where the world once stood
    the blades of grass
    cut me still

  • #3
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wow Vincent. You actually solved my problem with that- I was setting a new array in a recursive function (D'OH!) Thanks!

  • #4
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    what can I say? I solve problems even when I'm too lazy to look at things

    That function name scared the hell out of me...I was getting Alzheimer's reading it?
    Where the world once stood
    the blades of grass
    cut me still


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •