views:

128

answers:

3

Ok, I've been banging my head up against the wall on this and I have no clue why it isn't creating the element. Maybe something very small that I overlooked here. Basically, there is this Javascript code that is in a PHP document being outputted, like somewhere in the middle of when the page gets loaded, NOW, unfortunately it can't go into the header. Though I'm not sure that that is the problem anyways, but perhaps it is... hmmmmm.

// Setting the variables needed to be set.
    echo '
        <script type="text/javascript" src="' . $settings['default_theme_url'] . '/scripts/shoutbox.js"></script>';
    echo '
        <script type="text/javascript">
            var refreshRate = ', $params['refresh_rate'], ';
            createEventListener(window);
            window.addEventListener("load", loadShouts, false);

            function loadShouts()
            {
                var alldivs = document.getElementsByTagName(\'div\');
                var shoutCount = 0;
                var divName = "undefined";

                for (var i = 0; i<alldivs.length; i++)
                {
                    var is_counted = 0;
                    divName = alldivs[i].getAttribute(\'name\');

                    if (divName.indexOf(\'dp_Reserved_Shoutbox\') < 0 && divName.indexOf(\'dp_Reserved_Counted\') < 0)
                        continue;
                    else if(divName == "undefined") 
                        continue;
                    else
                    {
                        if (divName.indexOf(\'dp_Reserved_Counted\') == 0)
                        {
                            is_counted = 0;
                            shoutCount++;
                            continue;
                        }
                        else
                        {
                            shoutCount++;
                            is_counted = 1;
                        }
                    }

                    // Empty out the name attr.
                    alldivs[i].name = \'dp_Reserved_Counted\';

                    var shoutId = \'shoutbox_area\' + shoutCount;

                    // Build the div to be inserted.
                    var shoutHolder = document.createElement(\'div\');
                    shoutHolder.setAttribute(\'id\', [shoutId]);
                    shoutHolder.setAttribute(\'class\', \'dp_control_flow\');
                    shoutHolder.style.cssText = \'padding-right: 6px;\';
                    alldivs[i].parentNode.insertBefore(shoutHolder, alldivs[i]);

                    if (is_counted == 1)
                    {
                        startShouts(refreshRate, shoutId);
                        break;
                    }
                }
            }

        </script>';

Also, I'm sure the other functions that I'm linking to within these functions work just fine. The problem here is that within this function, the div never gets created at all and I can't understand why? Furthermore Firefox, FireBug is telling me that the variable divName is undefined, even though I have attempted to take care of this within the function, though not sure why.

Anyways, I need the created div element to be inserted just before the following HTML:

echo '
            <div name="dp_Reserved_Shoutbox" style="padding-bottom: 9px;"></div>';

I'm using name here instead of id because I don't want duplicate id values which is why I'm changing the name value and incrementing, since this function may be called more than 1 time. For example if there are 3 shoutboxes on the same page (Don't ask why...lol), I need to skip the other names that I already changed to "dp_Reserved_Counted", which I believe I am doing correctly. In any case, if I could I would place this into the header and have it called just once, but this isn't possible as these are loaded and no way of telling which one's they are, so it's directly hard-coded into the actual output on the page of where the shoutbox is within the HTML. Basically, not sure if that is the problem or not, but there must be some sort of work-around, unless the problem is within my code above... arrg

Please help me. Really what I need is a second set of eyes on this. Thanks :)

+2  A: 

When you're testing divName, switch the order of your conditions from this

                divName = alldivs[i].getAttribute(\'name\');

                if (divName.indexOf(\'dp_Reserved_Shoutbox\') < 0 && divName.indexOf(\'dp_Reserved_Counted\') < 0)
                    continue;
                else if(divName == "undefined") 
                    continue;

to this:

                var divName = alldivs[i].getAttribute(\'name\');
                if (!divName) // this is sufficient, by the way
                    continue;
                else if (divName.indexOf(\'dp_Reserved_Shoutbox\') < 0 && divName.indexOf(\'dp_Reserved_Counted\') < 0)
                    continue;

The problem is that when the script finds a div without a name, it tries to call the indexOf property of a non-existent value and therefore throws an error.

Casey Hope
don't need the if/elseif/else at all, the continues will break out of the current for iteration...
Tracker1
Wow, thanks I will give this a shout. You guys are AWESOME!
SoLoGHoST
Thank you that nipped divName right in the bud! BUT now I am getting startShouts is undefined at this line `startShouts(refreshRate, shoutId);` arrgggg, will post up the function, it's fairly small if you'd like to help me with it... THANK YOU VERY MUCH!
SoLoGHoST
The function is in the .js file that it is linked to, do you think that could be causing the problem??
SoLoGHoST
OUCH, just realized, it's adding the div element twice in the same spot... arggg.
SoLoGHoST
A: 
Tracker1
Ok, thanks for the attempt, but with your script, it continually loads forever and ever, there's gotta be a break in here to get out of the loop. Since we only need the first time it finds a `name="dp_Reserved_Shoutbox"`, after it finds it, it need to insert a div just before it and rename the name attribute to `"dp_Reserved_Counted"` (so that it doesn't count it again when the function gets called again), and than it needs to break outta here, which it's not doing... argg. I really appreciate this though, perhaps this can be fixed... hmmm.
SoLoGHoST
Ok, I added in a break at the very end, but now, just like before it does this twice in the very same spot, instead of in 2 different spots: `<div id="shoutbox_area1" class="dp_control_flow" style="padding-right: 6px;">`
SoLoGHoST
And instead of the area1, changing to area2 for the 2nd insert, it stays the same as area1, arggg.
SoLoGHoST
And it doesn't change the name attribute to "dp_Reserved_Counted".... argggggggg
SoLoGHoST
And just to let everyone know, this is perfect if you add a break; at the end of the function, but within the for loop, and than change `alldivs[i].name = "dp_Reserved_Counted";` to `alldivs[i].setAttribute("name", "dp_Reserved_Counted");`
SoLoGHoST
Well, atleast in my situation it's perfect, Cheers :)
SoLoGHoST
A: 

Ok, guys, just wanted to let you know how it went. And I thank both you greatly Tracker1 and Casey Hope. Especially Tracker for the excellent rewrite of the function. You guys ROCK. Here's the final function that I'm using bytheway, just a tiny bit of editing to Tracker1's Answer, which is why you got my vote hands down!

echo '
    <script type="text/javascript">
        var refreshRate = ' . $params['refresh_rate'] . ';
        createEventListener(window);
        window.addEventListener("load", loadShouts, false);

        function loadShouts()
        {
          var alldivs = document.getElementsByTagName("div");
          var shoutCount = 0;
          var divName = "undefined";

          for (var i = 0; i<alldivs.length; i++)
          {
            divName = alldivs[i].getAttribute("name");

            if (!divName)
              continue;
            if (divName.indexOf("dp_Reserved_Shoutbox") < 0 && divName.indexOf("dp_Reserved_Counted") < 0)
              continue;

            shoutCount++;    
            if (divName.indexOf("dp_Reserved_Counted") == 0)
              continue;

            // Empty out the name attr.
            alldivs[i].setAttribute("name", "dp_Reserved_Counted");

            var shoutId = "shoutbox_area" + shoutCount;

            // Build the div to be inserted.
            var shoutHolder = document.createElement("div");
            shoutHolder.setAttribute("id", shoutId);
            shoutHolder.setAttribute("class", "dp_control_flow");
            shoutHolder.style.cssText = "padding-right: 6px;";
            alldivs[i].parentNode.insertBefore(shoutHolder, alldivs[i]);

            startShouts(refreshRate, shoutId);
            break;
          }
        }

    </script>';

Thanks Again, you guys are the BEST!

SoLoGHoST