开发者

JS Error in IE 8... "Object Required"

开发者 https://www.devze.com 2023-03-13 21:05 出处:网络
I am implementing pagination on a project currently in progress but I am getting an error with this piece of code in IE8:

I am implementing pagination on a project currently in progress but I am getting an error with this piece of code in IE8:

var rows = document.getElementById(tableName).rows;

Here is the error:

Message: Object required
开发者_如何学编程

I am using this open source code for the pagination:

http://en.newinstance.it/2006/09/27/client-side-html-table-pagination-with-javascript/

Now my question would be, is this a valid piece of code for ie 8? if not what could I substitute to obtain the same results of the given piece of code? (or how can i fix this error :P)

If more information is needed, I'll try my best to provide.


It looks like document.getElementById(tableName) is not finding the table which you're expecting, and so it returns null. null.rows is not valid, and so there's an error there.

I'd recommend splitting that line into two and checking that the element is found before continuing:

var table = document.getElementById(tableName),
    rows;
if (table) {
    rows = table.rows;
} else {
    alert("Couldn't find table with id: " + tableName);
}


Better to use jquery for this. IF you use a jquery "if object is not found - no error will be returned"

Use it like this:

var table =  $('#tableName'); 
0

精彩评论

暂无评论...
验证码 换一张
取 消