Function is not defined .... but it is... (JS error / FF1.5)

Discuss how to use and promote Web standards with the Mozilla Gecko engine.
HaiDozo
Posts: 9
Joined: January 9th, 2006, 1:51 pm

Function is not defined .... but it is... (JS error / FF1.5)

Post by HaiDozo »

Hi,

I get a wierd javascript error in FF1.5.

It says a function is not defined even tho it is (code below).
This is a GreaseMonkey script.

Basically it's:
--
function abc(){

i++; //what ever here
setTimeout("abc()", 10000);

}
abc();
--

After 10 seconds I get "abc is not defined" error message.

Code: Select all

var obj;
   var bDidfind = false;
   var alist = new Array();
   var lPos = 0;
   var lAmount = 0;
   var i = 0;

   function SCX_Init(){
      i = 0;
      obj = document.getElementsByTagName('a');
      SCX_EnumLinks();
   }
   
   function SCX_EnumLinks(){
      i++;
      while(obj[i].href.indexOf("/folder/file.html") == -1){
         if (i < obj.length){
            i++;
         }else{
            if (bDidfind){
               window.location.reload();
            }else{
               setTimeout("window.location.reload()", 600000);
            }
            return;
         }
      }
      obj[i].target = "sametarget";
      e = document.createEvent("MouseEvents");
      e.initEvent("click", true, false);
      obj[i].dispatchEvent(e);
      
      setTimeout("SCX_EnumLinks()", 25000);
   }
   
   SCX_Init();


After 25 seconds I get an error saying SCX_EnumLinks is not defined, clearly it is defined as it was in there making the setTimeout().

Is this one of those "mysterious" errors or did I miss something? :P

Thanks!
HaiDozo
Posts: 9
Joined: January 9th, 2006, 1:51 pm

Post by HaiDozo »

I was reading the GreaseMonkey Docs and found out that all functions and variables dissappear after the script finishes. It say you should do:

window.abc = function(){...}

to make the function stick.
I did this but I still get the same error, what should I do?
User avatar
jqp
Posts: 5070
Joined: November 17th, 2004, 10:56 am
Location: In a box
Contact:

Post by jqp »

this works for me:

Code: Select all

unsafeWindow.abc = function() {
  alert("yes");
  setTimeout("window.abc()", 2000);
}
unsafeWindow.abc();

See the GM docs on the unsafeWindow object, which can explain it better than I.
HaiDozo
Posts: 9
Joined: January 9th, 2006, 1:51 pm

Post by HaiDozo »

Ahh! Sweet, it works!
Thank you jqp! :)
HaiDozo
Posts: 9
Joined: January 9th, 2006, 1:51 pm

Post by HaiDozo »

Oh another question:
In Opera UserJS you can do:
obj = document.getElementById("MyAnchorLink");
obj.click();


I'm referring to the obj.click() ('obj' being an anchor node).
The .click() doesn't work in FF, and I tried to do .dispatchEvent() as I wrote in my Code above, but this doesn't work. Well it kinda clicks the link (visually on the page) but no new window with the href-URL opens.

How can I click a link within the code?
User avatar
jqp
Posts: 5070
Joined: November 17th, 2004, 10:56 am
Location: In a box
Contact:

Post by jqp »

The click method is listed in the Gecko DOM reference, but I don't know why it doesn't work. The question has come up before, but the answer around here tends to be "don't do that".

You can ask in the Web Development forum here and probably get a better answer. (I assume you're still working on a user script and you'll probably want to mention that...)

Edit: umm... okay, this thread has either been moved or I forgot where it started... either way, it's here now.
HaiDozo
Posts: 9
Joined: January 9th, 2006, 1:51 pm

Post by HaiDozo »

Ok, so .click() is a official "by the rules" thing? I was thinking maybe it's some Opera specific stuff (which I don't like, I like to program by the book).

Do I understand it correctly that it's a bug in FF1.5 that .click() doesn't work?

Is there maybe an alternative? I tried the .dispatchEvent(), as I mentioned above, but it didn't work either. Maybe it's related to the .click() bug? (if it's a bug).

Any help / discussion appreciated! :)
Thanks.
HaiDozo
Posts: 9
Joined: January 9th, 2006, 1:51 pm

Post by HaiDozo »

Argh, I read more of the W3C Docs and it seems Anchors do not have a click() method ! :O

W3C Docu

Is this for real? An Anchor element, if any element, should have a click method, don't you agree?
User avatar
jqp
Posts: 5070
Joined: November 17th, 2004, 10:56 am
Location: In a box
Contact:

Post by jqp »

Huh. I thought that click() was a standard method of links, but I was wrong.

The only element that has a click method is the input element, which I assume could be used to submit a form.

Code: Select all

alert(someAnchorLink.click); // undefined
alert(someInputElement.click); // function click() { ... native code ... }


Another trick to keep handy:

Code: Select all

var obj = document.getElementsByTagName("a")[0]; 
// or whatever object you want
for(var prop in obj) {
document.write(prop + ": " + obj[prop] + "<br>");
}

That gives you a quick list of properties and methods for an object.
HaiDozo
Posts: 9
Joined: January 9th, 2006, 1:51 pm

Post by HaiDozo »

Thanks,
now if I could just find a way to click an anchor link...
User avatar
jhaygood86
Posts: 372
Joined: July 18th, 2003, 11:58 am
Location: Marietta, GA
Contact:

Post by jhaygood86 »

you can do it a roundabout way:

Code: Select all

window.location = anchor.href


but this breaks the back button, but that can be easily fixed by doing:

Code: Select all

var navigateForm = document.createElement("form");
navigateForm.method = "get";
navigateForm.action = anchor.href;
document.body.appendChild( navigateForm );
navigateForm.submit();
Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9
User avatar
jscher2000
Posts: 11772
Joined: December 19th, 2004, 12:26 am
Location: Silicon Valley, CA USA
Contact:

Post by jscher2000 »

jhaygood86 wrote:you can do it a roundabout way:

Code: Select all

window.location = anchor.href


but this breaks the back button, but that can be easily fixed by doing:

Hmmm, I thought you could go back after that, which is why I used window.location.replace() to overwrite the current page in history when I don't want a "back-forward" loop. I guess another workaround would be window.open?
HaiDozo
Posts: 9
Joined: January 9th, 2006, 1:51 pm

Post by HaiDozo »

I'm using window.open() currently as a workaround but I don't like hacks/dirty programming. I would prefer to do it correctly and beautifully.

I quess it just is not possible to click an anchor from within javascript code. hmmm.. but isn't .dispatchEvent() (with a proper click event) supposed to work?
User avatar
jhaygood86
Posts: 372
Joined: July 18th, 2003, 11:58 am
Location: Marietta, GA
Contact:

Post by jhaygood86 »

I wish! Would make my job a lot easier...
Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9
HaiDozo
Posts: 9
Joined: January 9th, 2006, 1:51 pm

Post by HaiDozo »

So... can I assume that .dispatchEvent() is bugged in FF1.5 and that it will work in the (hopefully near) future?
Post Reply