...

View Full Version : two links one click



wanderingsoul
01-15-2007, 10:02 PM
var tid = document.getElementsByName(id)[0].value;
document.getElementsBySelector(td a.index)[0].onclick = window.open("site/do.php?id=" + tid);


This is a Greasemonkey script. I'm trying to pull 5 numbers from "value", appending it to the end of a url and opening both the original link and the second link, with the second link opening in a new window.

Getting tid from:
<input type="hidden" name="id" value="#####" />

Adding onlick to a:
<td><a class="index" href="sit.php/#####/">Open</a></td>

Source for getElementsBySelector:
http://simon.incutio.com/js/getElementsBySelector.js

I get a "missing ) after argument list" in the javascript console. I'm not really sure what to do now...

david_kw
01-15-2007, 10:11 PM
It looks like that function takes a string so try adding quotes.

document.getElementsBySelector("td a.index")[0].onclick = window.open("site/do.php?id=" + tid);

david_kw

wanderingsoul
01-15-2007, 10:18 PM
It looks like that function takes a string so try adding quotes.

document.getElementsBySelector("td a.index")[0].onclick = window.open("site/do.php?id=" + tid);

david_kw

Thanks! I also forgot the quotes around "id". Now it's working great :)
Stupid mistakes... fun, fun, fun :P

Edit: Acutally, it works but not when I want it to.
I want it just to go when the link at X*X is clicked and not run as soon as the page loads.

X*X: <td><a class="index" href="sit.php/#####/">Open</a></td>

It's probably something minor again but how would I do this?

Right now, it opens the link ("site/do.php?id=" + tid) immediately when the page loads and again when the link is actually clicked...

david_kw
01-15-2007, 10:31 PM
Do you have a link to a sample page?

david_kw

wanderingsoul
01-15-2007, 11:03 PM
A sample, slightly changed from the original (html pages instead): http://trsi.50webs.com

The code: http://trsi.50webs.com/testing.user.js

Thanks for looking.

david_kw
01-15-2007, 11:12 PM
That sample has no javascript in it. Sorry, but I don't understand what you are trying to do and I don't understand what problem is occuring. Maybe the second page in your sample is supposed to include the js you linked in your post?

david_kw

wanderingsoul
01-15-2007, 11:25 PM
That sample has no javascript in it. Sorry, but I don't understand what you are trying to do and I don't understand what problem is occuring. Maybe the second page in your sample is supposed to include the js you linked in your post?

david_kw

Sorry, it's a user script for a private website. I want to change the behavior that I get when I click on a certain link. The javascript file I linked to is a Greasemonkey script. If Greasemonkey is installed, the script can be installed by clicking on that link and then the problem shows up.

After clicking the link on index page, l1.html will open. Clicking the link in l1.html should open /12345/somefile.html and do12345.html at the same time. Right now, with the script, do12345.html is opening immediately instead when l1.html loads.

david_kw
01-15-2007, 11:52 PM
I still don't understand how that script is supposed to fit in to the webpage. I hate guessing but I'll take one shot. If I'm wrong hopefully someone else can understand better than I what it is you are looking for.



<html>
<head>
<title>Test page</title>
<script type="text/javascript">
function openEm() {
var ref = document.getElementById("id1").href;
var tid = document.getElementById("id2").value;
window.open("site/do.php?id=" + tid);
window.location.href = ref;
}
</script>
</head>
<body>
<table><tr><td><a id="id1" class="index" href="/12345/somefile.html" onclick="openEm(); return(false);">Open</a></td></tr></table>
<input id="id2" type="hidden" name="id" value="12345" />
</body>
</html>


david_kw

wanderingsoul
01-16-2007, 12:30 AM
Thanks for the code. The problem is that I don't have access to the site to change the html to add id1 or id2 and onclick doesn't exist. Basically, I want onclick to be added to the link from a user script, not run as soon as the page is opened.

For example, if this code were for a forum, I might want the "Post reply" button to also subscribe me to the thread, so the javascript would add a second link to the "Post" button: one that would open in a new window to subscribe to the thread.

Sorry, but I can't think of another way to explain it.

Why doesn't:

document.getElementsBySelector("td a.index")[0].onclick = window.open("http://trsi.50webs.com/do" + tid + ".html");
just set the "onclick" for the link instead of doing it immediately?

It should be like this after:

<a class="index" href="/12345/somefile.html" onclick=window.open("do12345.html")>Open</a>

Anyway, thanks for taking all this time to help out.

david_kw
01-16-2007, 12:53 AM
Perhaps



document.getElementsBySelector("td a.index")[0].onclick = function() { window.open("http://trsi.50webs.com/do" + tid + ".html"); }

david_kw

wanderingsoul
01-16-2007, 01:36 AM
david_kw, thank you!

I knew it had to be something silly ;)

After changing the code, I got "component is not available", so I find that Greasemonkey doesn't like javascript with .onclick

Had to replace it with addEventListener.


var tid = document.getElementsByName("id")[0].value;
var dlink = document.getElementsBySelector("td a.index")[0];
dlink.addEventListener("click",function() { window.open("http://trsi.50webs.com/do" + tid + ".html"); },true);

Many thanks for the help and for sticking it out to get this code debugged :thumbsup:

david_kw
01-16-2007, 03:00 AM
Welcome. :)

Unfortunately, I think addEventListener doesn't work in IE. If I'm right try looking at QuirksMode for a cross platform function.

http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html

david_kw

wanderingsoul
01-16-2007, 03:22 AM
Welcome. :)

Unfortunately, I think addEventListener doesn't work in IE. If I'm right try looking at QuirksMode for a cross platform function.

http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html

david_kw

That's OK. Greasemonkey is a Firefox-only extension so I didn't need any cross browser compatibility, just so long as Firefox is happy. Thanks also for the link. I'll bookmark that in case I need to add IE support for this in the future :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum