tags:

views:

76

answers:

1

I'm running the following javacript below to place horizontal scrolling text on the banner of my website. it works in on server and not another. I get the following error:

Error: 'this.mqo' is null or not an object

JavaScript:

function start() {
    new mq('m1');
/* new mq('m2');
    */
    mqRotate(mqr); // must come last
}
window.onload = start;

// Continuous Text Marquee
// permission to use this Javascript on your web page is granted
// provided that all of the code below in this script (including these
// comments) is used without any alteration

function objWidth(obj) {
    if (obj.offsetWidth) return obj.offsetWidth;
    if (obj.clip) return obj.clip.width;
    return 0;
}
var mqr = [];

function mq(id) {
    this.mqo = document.getElementById(id);
    var wid = objWidth(this.mqo.getElementsByTagName('span')[0]) + 5;
    var fulwid = objWidth(this.mqo);
    var txt = this.mqo.getElementsByTagName('span')[0].innerHTML;
    this.mqo.innerHTML = '';
    var heit = this.mqo.style.height;
    this.mqo.onmouseout = function () {
        mqRotate(mqr);
    };
    this.mqo.onmouseover = function () {
        clearTimeout(mqr[0].TO);
    };
    this.mqo.ary = [];
    var maxw = Math.ceil(fulwid / wid) + 1;
    for (var i = 0; i < maxw; i++) {
        this.mqo.ary[i] = document.createElement('div');
        this.mqo.ary[i].innerHTML = txt;
        this.mqo.ary[i].style.position = 'absolute';
        this.mqo.ary[i].style.left = (wid * i) + 'px';
        this.mqo.ary[i].style.width = wid + 'px';
        this.mqo.ary[i].style.height = heit;
        this.mqo.appendChild(this.mqo.ary[i]);
    }
    mqr.push(this.mqo);
}
function mqRotate(mqr) {
    if (!mqr) return;
    for (var j = mqr.length - 1; j > -1; j--) {
        maxa = mqr[j].ary.length;
        for (var i = 0; i < maxa; i++) {
            var x = mqr[j].ary[i].style;
            x.left = (parseInt(x.left, 10) - 1) + 'px';
        }
        var y = mqr[j].ary[0].style;
        if (parseInt(y.left, 10) + parseInt(y.width, 10) < 0) {
            var z = mqr[j].ary.shift();
            z.style.left = (parseInt(z.style.left) + parseInt(z.style.width) * maxa) + 'px';
            mqr[j].ary.push(z);
        }
    }
    mqr[0].TO = setTimeout('mqRotate(mqr)', 10);
}
+4  A: 

The reason is most likely that there is no element with the id "m1". Place this line first in the start function to diagnose this:

alert(document.getElementById('m1'));

If it shows "[Object]" (or similar), the element exists and it's some other problem, but if it shows "undefined" it means that there is no such element in the page.

Guffa
Why would it work on one server and not the other?
GM2
@GM2: is it possible your HTML markup is different on each server?
Andy E
@GM2: Because there is something on the servers that causes something in the HTML code to be different. The client script has nothing to do with the server, so the difference has to be evident in the page that runs in the browser.
Guffa
I placed the alert within the function and it's prompting a "null" on page load.
GM2
@GM2: That's right... The method returns `null` instead of `undefined`. It means that there is no element with the id "m1" in the page. View the page source in the browser, locate the element that you want to use, and check what it's actual id is.
Guffa
YEs, there is an div id with "m1". here's the line of code in the template file. <ItemTemplate> <div id="m1" class="marquee"><span><%# Eval("Test_Text") %></span></div> </ItemTemplate>
GM2
@GM2: Look at the source of the page when it has loaded in the browser, not in the source code. There is no guarantee that the tag ends up in the final code just because it's in the source, or that it looks the same if it's actually there. Also check if there is only one element with that id; an id should be unique, so if you have more than one element (as suggested by the use of an item template tag) with the same id, the code is invalid.
Guffa
you're right, the m1 id is not displayed in the source view of the rendered page.
GM2
@GM2: Then it depends on whether there should always be such an element or not. If it shouldn't, then you can use `if (document.getElementById('m1')) { ... }` around the code inside the `start` function to skip the marquee if the element isn't there.
Guffa
like this.......... function start() if (document.getElementById('m1')) { new mq('m1'); mqRotate(mqr); // must come last }
GM2
@GM2: Almost... Inside the `{}` brackets for the function body: `function start() { if (document.getElementById('m1')) { new mq('m1'); mqRotate(mqr); } }`
Guffa
i ran this code but no luck. thanks a lot for your help.
GM2
@GM2: What happened when you tried it? Did you get any error message?
Guffa