Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Jan 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    two links one click

    Code:
    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...

  • #2
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    New Coder
    Join Date
    Jan 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by david_kw View Post
    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...
    Last edited by wanderingsoul; 01-15-2007 at 11:29 PM.

  • #4
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Do you have a link to a sample page?

    david_kw

  • #5
    New Coder
    Join Date
    Jan 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    New Coder
    Join Date
    Jan 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by david_kw View Post
    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.
    Last edited by wanderingsoul; 01-16-2007 at 12:28 AM.

  • #8
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    <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

  • #9
    New Coder
    Join Date
    Jan 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    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:
    Code:
    <a class="index" href="/12345/somefile.html" onclick=window.open("do12345.html")>Open</a>
    Anyway, thanks for taking all this time to help out.

  • #10
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Perhaps

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

  • #11
    New Coder
    Join Date
    Jan 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    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

  • #12
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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/archi..._winner_1.html

    david_kw

  • #13
    New Coder
    Join Date
    Jan 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by david_kw View Post
    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/archi..._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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •