LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   javascript (https://www.linuxquestions.org/questions/programming-9/javascript-861734/)

katea 02-09-2011 09:37 PM

javascript
 
hello,

I'm trying to make a very small script with greasemonkey to do some automatic things when a page loads.
The first thing I need to do is to click on a link. I've done that.
Then I need to select the 3rd element of a drop down list that appears a few moments (depending on the connection) after the link is pressed, without reloading the page (ajax or something like that). This I can't get to work, can you help me?
The select has no id or class, but I can get its xpath value. There is only one select on the page.

I've tried both setTimeOut and the DOMNodeInserted event with no effect.

Code:

// ==UserScript==
// @name          test
// @namespace      "www.google.com"
// @include        www.mywebpage.com
// ==/UserScript==

// click:
location.assign( "javascript:" + "f(x)" + ";void(0)" );

// the rest is supposed to wait until the select appears, and then select its second element, but this doesn't work:
setTimeout("func1()",100);
window.func1 = function()
{
if (document.getElementsByTagName("select") == null){
        setTimeout("func1()",100);
}
else{
        var drop = document.getElementsByTagName("select");
        (drop[0]).selectedIndex=0;
}
}

Thanks.

smoker 02-09-2011 11:40 PM

Surely the required selected index will be 2 ?

katea 02-10-2011 01:29 AM

Yes you're right :)
but unfortunately it's just a typo, which means that it's not where the error comes from.
I'm just a beginner so I don't really even know how to debug js (I'm just using notepad), but if I go to the error java console I can see "'func1' is not defined."

smoker 02-10-2011 02:37 PM

Well define func1.

Here's a tip :
window.func1 isn't the declaration you think it is.

func1 = WHAT ?

What is the type of the variable ?
Where have you said what it is supposed to be ?

Maybe var func1 = window.function() .... ?

katea 02-10-2011 04:11 PM

var func1 = window.function() { ... } doesn't work either.
It still says func1 is not defined.
Please don't forget that this is not a regular js, it's greasemonkey, which adds some differences. I just picked this window.func1 = function()
from the net (the book on greasemonkey). But since it's not working, I'm opened to any suggestions.

Please help me.
Thanks.

katea 02-11-2011 02:09 PM

I've tried quite a lot of things but none worked. Is there someone here who knows a bit about greasemonkey?

katea 02-12-2011 01:48 AM

Can I get help on this please ?
I'm really a beginner but I would very much appreciate this to work for my personal use.

smoker 02-12-2011 04:00 AM

Functions must be declared before they are used. Your script as above appears to call a function, func1() before any function is declared or described.
Code:

// the rest is supposed to wait until the select appears, and then select its second element, but this doesn't work:
setTimeout("func1()",100);


ntubski 02-12-2011 09:51 AM

Why do you pass a string to setTimeout? The following works for me under Greasemonkey:

Code:

window.func1 = function()
{
    alert("called func1");
}
setTimeout(window.func1,100);


katea 02-12-2011 09:02 PM

okay, thank you, it works now that I've not passed the string :).
Now my next problem: actually it just selected the correct element from the drop down menu, but nothing happened.
The element is declared like this: <select onChange="f(this.options[selectedIndex].value)">
How could I fire that event ? I can't call f with
location.assign( "javascript:" + "f(x)" + ";void(0)" );
because 'this' is not the same.

Thanks.

katea 02-13-2011 07:00 PM

Can someone help me with that new situation please?
Thanks

ntubski 02-16-2011 06:43 PM

I don't understand the situation. Can you post an entire script and html page so I can reproduce the problem?


All times are GMT -5. The time now is 04:59 AM.