English 中文(简体)
How to increase speed of getElementById() function on IE or give other solutions?
原标题:

I have a project using Javascript parse json string and put data into div content.

In this case, all of itemname variables is the same div s id.

If variable i about 900, I run this code in Firefox 3 for <10ms, but it run on IE 7 for >9s, IE process this code slower 100 times than Firefox

I don t know what happen with IE ?

If I remove the line document.getElementById(itemname), speed of them seems the same.

The main problem arcording to me is document.getElementById() function?

Could you show me how to solve this prolem to increase this code on IE ?

Thank in advance.

            var i = obj.items.length-2;
            hnxmessageid = obj.items[i+1].v;

            do{
                itemname = obj.items[i].n;
                itemvalue = obj.items[i].v;
                document.getElementByid(itemname);
                i--;
            }while(i>=0);
问题回答

Are you really noticing any latency?

gEBI is natively very very fast, I don t think you can avoid it anyway for what you re doing. Could you provide a low-down of what you re doing precisely? It looks like you re using a loop, but can you post exactly what you re doing inside of the loop, what your common goal of the script is?

document.getElementByid(itemname) is the fastest way to get a single element from the DOM in any real application you will sould not see any problems with using it, if you do see a problem you need to rethink you code a little it possible to acomplish any task with just a handfull of calls for this method. You can present you full problem if you like so I could show you an example

At least cache the reference to document:

var doc = document;
for(;;) { 
   doc.getElementById(..);
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签