Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-05-2009, 05:32 PM   PM User | #1
Plytas
New Coder

 
Join Date: Mar 2009
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Plytas is an unknown quantity at this point
How to find a link and click it (need help)?

Hello everyone,

First of all, I'm not a noob on javascript (I know the basics), but I need your help.
What i need, is to make the script to find a link, and click it. In example...
There is a page, with 10 links, everytime you load the page, the links changes (it starts like normal, but has diferent endings), so i need the script to find the link that i need, and click it.
It would be great if you help me

Thanx,
Plytas
Plytas is offline   Reply With Quote
Old 03-05-2009, 05:42 PM   PM User | #2
Eldarrion
Regular Coder

 
Join Date: Feb 2009
Location: Wheeling, IL
Posts: 358
Thanks: 5
Thanked 62 Times in 60 Posts
Eldarrion is on a distinguished road
Easiest way would be to assign IDs to your links that will remain unchanged and the script would then use getElementById("idoflink") and click on that. Hope this helps.
Eldarrion is offline   Reply With Quote
Old 03-05-2009, 05:44 PM   PM User | #3
Plytas
New Coder

 
Join Date: Mar 2009
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Plytas is an unknown quantity at this point
The problem is, that I can't change the page. I can make only script.
Plytas is offline   Reply With Quote
Old 03-05-2009, 05:45 PM   PM User | #4
Eldarrion
Regular Coder

 
Join Date: Feb 2009
Location: Wheeling, IL
Posts: 358
Thanks: 5
Thanked 62 Times in 60 Posts
Eldarrion is on a distinguished road
In which case it would be nice if we could see the code we have to work on. I mean, if it does assign IDs already, that can be used... or if it assigns names to the links, etc.
Eldarrion is offline   Reply With Quote
Old 03-05-2009, 05:51 PM   PM User | #5
Plytas
New Coder

 
Join Date: Mar 2009
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Plytas is an unknown quantity at this point
There is the page code:
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml><card id="index" title="MetalScape 19:47 03-05">
<p align="center"><small><a href="zaisti.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=use_points">Turi 26 nepanaudotus lygio taskus</a><br/>
	&gt;&lt;&gt; * &lt;&gt;&lt;<br/></small></p>

<p align="center"><small>
<b>Tu miske, koki medi kirsi?</b><br/>
[x kiek_kartu] (minim medkircio lygis)<br/>
&gt;&lt;&gt; * &lt;&gt;&lt;<br/>Jusu medkirtystes lygis: <b>218435.1</b><br/></small></p>

<p align="left"><small>
#<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=alk&amp;kd=52428">Alksnio</a> [<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=alk&amp;kd=52428&amp;kiek=40">x40</a>] (0)<br/>
#<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=iev&amp;kd=52428">Ievos</a> [<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=iev&amp;kd=52428&amp;kiek=20">x20</a>] (50)<br/>
#<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=glu&amp;kd=52428">Gluosnio</a> [<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=glu&amp;kd=52428&amp;kiek=10">x10</a>] (300)<br/>
#<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=top&amp;kd=52428">Topolio</a> [<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=top&amp;kd=52428&amp;kiek=7">x7</a>] (1000)<br/>
#<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=kle&amp;kd=52428">Klevo</a> [<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=kle&amp;kd=52428&amp;kiek=5">x5</a>] (3000)<br/>
#<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=azu&amp;kd=52428">Azuolo</a> [<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=azu&amp;kd=52428&amp;kiek=3">x3</a>] (5000)<br/>
#<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=uos&amp;kd=52428">Uosio</a> [<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=uos&amp;kd=52428&amp;kiek=3">x3</a>] (8000)<br/>
#<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=mau&amp;kd=52428">Maumedzio</a> [<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=mau&amp;kd=52428&amp;kiek=3">x3</a>] (11000)<br/>
#<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=bao&amp;kd=52428">Baobabo</a> [<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=bao&amp;kd=52428&amp;kiek=3">x3</a>] (15000)<br/>
#<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=rau&amp;kd=52428">Raudonmedzio</a> [<a href="miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=kertu&amp;ka=rau&amp;kd=52428&amp;kiek=2">x2</a>] (30000)<br/>

</small></p>
<p align="center"><small>
&gt;&lt;&gt; * &lt;&gt;&lt;<br/><a href="meniu.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=">I pradzia</a><br/><b>Miskas:</b><br/>
plytas<br/>

</small>
<input name="chat" type="text" maxlength="100" title="Chat" value=""/><br/>
            <anchor>Sakyk<go href="/miskas.php?w=&amp;nick=plytas&amp;pass=2a6848e1cb92e0985dabc65cbcd1b52f&amp;id=med" method="post">
        <postfield name="chat" value="$(chat)"/>
        </go></anchor><small></small></p></card></wml>
As you can see, there are the links, every time i load this page, the link ending changes.
And i can't change this code.

BTW. the text is in lithuanian
Plytas is offline   Reply With Quote
Old 03-05-2009, 06:35 PM   PM User | #6
Eldarrion
Regular Coder

 
Join Date: Feb 2009
Location: Wheeling, IL
Posts: 358
Thanks: 5
Thanked 62 Times in 60 Posts
Eldarrion is on a distinguished road
Going through all the childnodes proved to be rather annoying, but in the end:

Code:
<script type="text/javascript">
    function selectlink() {
        p = document.body.childNodes;
        small = p[5].childNodes;
        links = small[0].childNodes;
        clicky = links[3].href;
        window.location=clicky;
    }
</script>
This essentially points the browser to the link, called x40 in the code you provided me. There probably is a better way to go about it (with a loop that runs through the entire child nodes list there and looks for a specific link that is called... "Alksnio" for an example, but... this should do the trick, provided the link you need to click has the same position every time the web page is generated) Give it a whirl, see how it works for you. Maybe someone else will give a better solution in the mean-time.
Eldarrion is offline   Reply With Quote
Old 03-06-2009, 06:20 PM   PM User | #7
Plytas
New Coder

 
Join Date: Mar 2009
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Plytas is an unknown quantity at this point
Hmm... I guess I am noob
How can I get it to work?
Plytas is offline   Reply With Quote
Old 03-06-2009, 06:36 PM   PM User | #8
Eldarrion
Regular Coder

 
Join Date: Feb 2009
Location: Wheeling, IL
Posts: 358
Thanks: 5
Thanked 62 Times in 60 Posts
Eldarrion is on a distinguished road
Actually, here's a much better solution:

Code:
<script type="text/javascript">
	function selectlink() {
		p = document.getElementsByTagName("a")
		clicky = p[1].href;
		window.location = clicky;
	}
</script>
All in all, choosing which link to go to is in the line "clicky = p[1].href;". Just alter the number there to point to the specific link. This would work... but once again depends on the link being in the same position all the time. As to making it actually do what it's supposed to... I guess assign an onclick event to a button that will trigger the function like this:

Code:
<input type="button" onclick="selectlink()" value="test" />
If you need more assistance, don't hesitate to ask.
Eldarrion is offline   Reply With Quote
Old 03-06-2009, 06:52 PM   PM User | #9
Plytas
New Coder

 
Join Date: Mar 2009
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Plytas is an unknown quantity at this point
The thing is that, the page is not mine...
I can only make js file...
Plytas is offline   Reply With Quote
Old 03-06-2009, 06:54 PM   PM User | #10
Eldarrion
Regular Coder

 
Join Date: Feb 2009
Location: Wheeling, IL
Posts: 358
Thanks: 5
Thanked 62 Times in 60 Posts
Eldarrion is on a distinguished road
When do you want the function to activate? It certainly helps knowing that.
Eldarrion is offline   Reply With Quote
Old 03-06-2009, 06:58 PM   PM User | #11
Plytas
New Coder

 
Join Date: Mar 2009
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Plytas is an unknown quantity at this point
When the page loads...
I could use redirect function, but link changes...
Plytas is offline   Reply With Quote
Old 03-06-2009, 07:12 PM   PM User | #12
Eldarrion
Regular Coder

 
Join Date: Feb 2009
Location: Wheeling, IL
Posts: 358
Thanks: 5
Thanked 62 Times in 60 Posts
Eldarrion is on a distinguished road
In which case the code would look like:

Code:
<script type="text/javascript">
	window.onload = function selectlink() {
		p = document.getElementsByTagName("a")
		clicky = p[1].href;
		window.location = clicky;
	}
</script>
This would force it to follow the link when the page loads. Once again, it relies on the link you want followed being in the same location all the time, and ofcourse, altering the number in "clicky = p[1].href;" will designate the specific link. Hope this helps.
Eldarrion is offline   Reply With Quote
Users who have thanked Eldarrion for this post:
Plytas (03-06-2009)
Old 03-06-2009, 07:51 PM   PM User | #13
Plytas
New Coder

 
Join Date: Mar 2009
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Plytas is an unknown quantity at this point
I guess I won't make this to work... I create js file:
Code:
// ==UserScript==
// @name           name
// @namespace      namespace
// @include        http://ms.miestas.biz/miskas.php?w=&nick=plytas&pass=2a6848e1cb92e0985dabc65cbcd1b52f&id=med
// ==/UserScript==

window.onload = function selectlink() {
	p = document.getElementsByTagName("Alksnio")
	clicky = p[2].href;
	window.location = clicky;
}
But it won't work... Am I that bad?
Plytas is offline   Reply With Quote
Old 03-06-2009, 07:58 PM   PM User | #14
Eldarrion
Regular Coder

 
Join Date: Feb 2009
Location: Wheeling, IL
Posts: 358
Thanks: 5
Thanked 62 Times in 60 Posts
Eldarrion is on a distinguished road
One major problem that I see in your code (marked in red):

Code:
// ==UserScript==
// @name           name
// @namespace      namespace
// @include        http://ms.miestas.biz/miskas.php?w=&nick=plytas&pass=2a6848e1cb92e0985dabc65cbcd1b52f&id=med
// ==/UserScript==

window.onload = function selectlink() {
    p = document.getElementsByTagName("Alksnio")
    clicky = p[2].href;
    window.location = clicky;
}
There is a reason for the "a" tag - it points to anchor elements, whereas there is no "Alksnio" tag in HTML, so you're essentially... selecting nothing. And from that point on... the entire script would break and even get a pretty error in FireBug.
Eldarrion is offline   Reply With Quote
Old 03-06-2009, 08:24 PM   PM User | #15
Plytas
New Coder

 
Join Date: Mar 2009
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Plytas is an unknown quantity at this point
Still no efect... I load the page, but nothing hapens...
Plytas is offline   Reply With Quote
Reply

Bookmarks

Tags
click, find, link

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:46 PM.


Advertisement
Log in to turn off these ads.