views:

381

answers:

1

I have recently made the switch from HTML to XHTML... don't get me started on the "why" - let's just say it wasn't an option. Anyhoo...

This function worked fine in IE and FF when I was HTML, but now with XHTML I get the following error in FF:

heightElement is undefined

now here is the statement where I define heightElement

function revBars(isEFBOutput, isIXPPreview) {
  if (!isIXPPreview) isIXPPreview = false;
  var heightElement =
    $('<div id="resizeDetectingElement" style="position:absolute; left:-9999999px height:1em;">&#160;</div>')
      .prependTo('body')
      .get(0);
  heightElement.currentHeight = heightElement.offsetHeight;  // FireBug says the error is here.
  insert();
  window.onresize = refresh;
  setInterval(
 function() {
     if (heightElement.currentHeight != heightElement.offsetHeight) {
         heightElement.currentHeight = heightElement.offsetHeight; refresh();
     }
 }, 500);

function insert() {
    var px = "px";
    var color = (document.styleSheets[0].href.match("ftidStyleDay")) ? "black" : "white";
    $revMarks = $('.RevMark').removeClass('RevMark').addClass('UnMarked');
    if (!$revMarks.length) $revMarks = $('.UnMarked');
    $revMarks.each(function() {
        $('<div class="RevBar" />').css({
            'background-color': color,
            'width': '2px', 'position': 'absolute',
            'left': (isEFBOutput && !isIXPPreview ? '.25em' : '-.75em'),
            'height': this.offsetHeight,
            'top': offset(this) + px
        }).prependTo('body');
    });
}

function refresh() { $('.RevBar').remove(); insert(); }

function offset(obj) {
    var top = 0;
    if (obj.offsetParent) {
        do {
            top += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    if (document.all && (!isEFBOutput || (isEFBOutput && isIXPPreview))) top -= 15;
    return top;
}

}

any ideas why this is throwing an error in XHTML in FF? It still works in IE.

EDIT: Again, this code worked perfectly in FF and IE until I switched to XHTML. Firefox still creates a DOM for XHTML, right?

+1  A: 

I rewrote some of your example, as you have errors and is incomplete with function calls that are not in the example:

$(document).ready( function(){
    function revBars(isEFBOutput, isIXPPreview){
        var test = "<div id=\"someElement\">whatever</div>";
        $('body').prepend(test).get(0);
        var heightElement = $('#someElement');
        console.log( heightElement);
    }
    revBars();
});

At this point the heigthElement exists and the div is added to DOM. Please provide correct problems in order to get them solved.

UPDATES

//var heightElement = $('<div id="resizeDetectingElement">whatever</div>').prependTo('body').get(0); 
// this statement doesn't work
var heightElement = $('body').prepend('<div id="resizeDetectingElement">whatever</div>'); 
// this does
console.log(heightElement);

This is basically the same as first example.

The request that you made is to add a div before the element body and then get the first element of the body. The prependTo already returns the matched items so the get was irelevant. And you are attaching an element before the body so is normal that your variable will be empty.

Try my code and remove the downvote as not to many people had this kind of patience with your supercode.

UPDATE 2 This is an usage example for prepend() and prependTo().

<html>
<head>
    <script type="text/javascript" src="../lib/jquery/jquery-1.3.2.min.js"></script>
    <script type="text/javascript">
    $(document).ready( function(){
        $("#test_A").prepend('<div id=\'test_B\'>B</div>');
        $("#test_C").prependTo( $('#test_A'));
        var x = $('<div id="resizeDetectingElement">D</div>').prependTo('body').get(0);
        console.log( x ); // returns the div
        $(x).append('some text'); //works
    });
</script>
</head>
<body>
<div id='test_A'>A</div>
<div id='test_C'>C</div>
</body>
</html>

I hope this will help to make your code work. USE $(heightElement) not just heightElement which is the div not the jQuery object.

Elzo Valugi
You incorrectly assume that I want the function to run on $(document).ready.Your <code>.get(0)</code> doesn't do anything - it's never assigned to a variable, or manipulated. <code>$('body').prepend(test);</code> does the same thing. Could you be more specific than <code>you have errors</code>? Where? What errors? I am trying to solve this and "you have errors" is less than helpful.<code>Please provide correct problems</code>? What is incorrect? I am looking for assistance, and phrasing my problem as best I can.
ScottSEA
Wish I could edit comments - didn't realize that <code></code> doesn't work in comments. Mea Culpa Maxima.
ScottSEA
@ScottSEA your function is not ending, has parameters that are not used and your example is using calls to undefined functions. Perhaps IE is not set to show you javascript errors, but the code that you posted has at least a few.
Elzo Valugi
@Elzo Valugi: cool. I have posted the entire function (and nested functions as well). If you could point out any errors in my code specifically, I would appreciate it.
ScottSEA
@ScottSEA my IDE sais that you are missing a bracket before the insert function, after first function body
Elzo Valugi
@Elzo - the bracket wasn't indented four spaces, so got dropped off the code block. It is actually at the very end of the block - insert(), refresh() and offset() are child functions of revBars() function.
ScottSEA
@ScottSEA ok . check updates
Elzo Valugi
@Elzo: I really appreciate your patience, and your assistance. My understanding of prependTo may be flawed - it was my (mis)understanding that $('x').prependTo('y') is the exact same as $('y').prepend('x'). Is this incorrect?
ScottSEA
@Elzo: I used .get(0) to retrieve the actual DOM element, not a jQuery object so I could use .currentHeight and .offsetHeight properties of the DOM, rather than jQuery function.
ScottSEA
@ScottSEA I made you a complete working example for both functions. Good luck
Elzo Valugi
@Elzo: re: my "supercode" - bite my bender. Your snarky comments (ie "provide correct problems in order to get them solved") are why I downvoted your answer. You may have been coding perfect JavaScript and jQuery out of the womb, but we mortals must learn it one step at a time. Thank you sincerely for your help and constructive criticism, but keep the sarcasm and derision - I get enough of that from my wife.
ScottSEA