[BUG?] Javascript getElementById()

Discussions and Tech Support specific to the iMacros Firefox add-on.
Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information: CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
step5
Posts: 14
Joined: Wed Jun 18, 2008 7:58 am

[BUG?] Javascript getElementById()

Post by step5 » Wed Jun 17, 2009 9:28 am

Hi@all,

i made a javascript macro which basically consists of a for() loop, an if() inside the loop and a setTimeout inside the if().
The macro should check every second for a <div> with the name "validated" to be displayed with the attribute style="display:block"
If it exceeds about 140 trys it should refresh the page.

Code: Select all

var closetab;
var refreshtab;
var retrys=0;

closetab  = "CODE:\r\n";
closetab += "TAB CLOSE";

refreshtab  = "CODE:\r\n";
refreshtab += "REFRESH";

for (var i=0;i<1;i++)
{
	if (retrys<140)
	{
		if (document.getElementById('validated').style.display == 'block')
		{
			iimPlay(closetab);
		}
		else
		{
			setTimeout(i=i-1,1000);
			retrys++;
		}
	}
	else
	{
		iimPlay(refreshtab);
	}
}
However i get this error:
TypeError: document.getElementById("validated") is null, line xx

If i write this in the address bar it displays 'block':
javascript:alert(document.getElementById('validated').style.display);

So the element is there 100% and it has content.

Sorry but i can't give you a sample link because the target page is dynamically generated inside an account.
Maybe you create yourself a sample page with a <div id="validated" style="display:none">Test<div> and a javascript timeout to set it to block with document.getElementById('validated').style.display='block';

VERSION BUILD=6210326 RECORDER=FX

Thank you!
step5
Posts: 14
Joined: Wed Jun 18, 2008 7:58 am

Re: [BUG?] Javascript getElementById()

Post by step5 » Wed Jun 24, 2009 3:40 pm

So nobody can help me?

OK. I put up a simple example:

Go to http://mtph.madpage.com/imacros_test.php

Here the Code. Please validate with the page above.

Code: Select all

var closetab;

closetab  = "CODE:\r\n";
closetab += "TAB CLOSE";


if (document.getElementById('validated').style.display == 'block')
		{
			iimPlay(closetab);
		}
name it 'something.js'

Play it and voilá you get this error:
TypeError:document.getElementById("validated") is null, line 7

If i try this in the address bar it works like a charm:
javascript:if(document.getElementById('validated').style.display=='block')alert('OK');
Last edited by step5 on Thu Oct 08, 2009 8:59 am, edited 1 time in total.
step5
Posts: 14
Joined: Wed Jun 18, 2008 7:58 am

Re: [BUG?] Javascript getElementById()

Post by step5 » Thu Oct 08, 2009 8:58 am

This Problem still exists with
VERSION BUILD=6240709 RECORDER=FX

Try the simple example in Post #2
josephconlin
Posts: 190
Joined: Wed Aug 06, 2008 2:38 am

Re: [BUG?] Javascript getElementById()

Post by josephconlin » Thu Oct 08, 2009 6:50 pm

step5 wrote:This Problem still exists with
VERSION BUILD=6240709 RECORDER=FX

Try the simple example in Post #2
These two forum posts might help.
http://forum.imacros.net/viewtopic.php?f=11&t=7545
http://forum.imacros.net/viewtopic.php?f=11&t=7609

It appears to me that you need to use the content object and parse the page rather than accessing it directly. It seems like I had read somewhere that iMacros document object refers to a different object than the webpage, so I'm guessing that the content object refers to the page itself. You might try searching the forums a little more thoroughly than I did.

Hope this helps.
step5
Posts: 14
Joined: Wed Jun 18, 2008 7:58 am

Re: [BUG?] Javascript getElementById()

Post by step5 » Wed Dec 09, 2009 8:27 am

Hi, thank u i will try that. I think i will need some time but i will reply if it works.
ABooth
Posts: 223
Joined: Mon Aug 10, 2009 4:25 pm

Re: [BUG?] Javascript getElementById()

Post by ABooth » Wed Dec 09, 2009 4:44 pm

A Javascript macro is not part of the page. It is run from within the plug-in and therefore does not have access to the page's DOM. That's why we have IIM macros and the iimExtract command. They access elements of the document of the currently focused tab.

If you want to select an element, you either use the IIM TAG command with EXTRACT= to get info from it, TAG command with Content= to put text into it, TAG EXTRACT on <HTML> element to grab the entire page and parse it, or execute some javascript on the page using the iim TAG command to click an element.
iMacros for Firefox supports JavaScript Macros (Scripting)
JavaScript supports Java via LiveConnect

Therefore: You can write powerful macros with iMacros for Firefox. Have a look at this one

Post feature requests here. Maybe one day, they'll pin it?
amit.tanwar8524
Posts: 1
Joined: Fri Oct 14, 2011 7:10 am

Re: [BUG?] Javascript getElementById()

Post by amit.tanwar8524 » Fri Oct 14, 2011 7:12 am

to access the page DOM use this

Code: Select all

window.content.document.getElementById('YourId');
Post Reply