MozillaZine


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

Post Posted January 9th, 2006, 2:05 pm

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 Posted January 9th, 2006, 2:46 pm

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?

jqp

User avatar
 
Posts: 5070
Joined: November 17th, 2004, 10:56 am
Location: In a box

Post Posted January 9th, 2006, 3:08 pm

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 Posted January 9th, 2006, 5:45 pm

Ahh! Sweet, it works!
Thank you jqp! :)

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

Post Posted January 9th, 2006, 7:55 pm

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?

jqp

User avatar
 
Posts: 5070
Joined: November 17th, 2004, 10:56 am
Location: In a box

Post Posted January 9th, 2006, 8:09 pm

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 Posted January 10th, 2006, 10:24 am

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 Posted January 10th, 2006, 10:30 am

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?

jqp

User avatar
 
Posts: 5070
Joined: November 17th, 2004, 10:56 am
Location: In a box

Post Posted January 10th, 2006, 10:46 am

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 Posted January 10th, 2006, 1:07 pm

Thanks,
now if I could just find a way to click an anchor link...

jhaygood86

User avatar
 
Posts: 372
Joined: July 18th, 2003, 11:58 am
Location: Marietta, GA

Post Posted January 11th, 2006, 8:40 pm

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

jscher2000

User avatar
 
Posts: 11653
Joined: December 19th, 2004, 12:26 am
Location: Silicon Valley, CA USA

Post Posted January 12th, 2006, 2:26 pm

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 Posted January 12th, 2006, 4:31 pm

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?

jhaygood86

User avatar
 
Posts: 372
Joined: July 18th, 2003, 11:58 am
Location: Marietta, GA

Post Posted January 13th, 2006, 9:41 pm

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 Posted January 16th, 2006, 10:27 am

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

Return to Web Development / Standards Evangelism


Who is online

Users browsing this forum: No registered users and 0 guests