views:

1347

answers:

3

This is the same question as THIS ONE, I can't answer that anymore, so I'm re-posting it with my account.
Sorry for the mess.

I need a Greasemonkey script that on a page load activates a href link like 'javascript:FUNCTION'. I've seen this code:

<script language="Javascript" type="text/javascript">
    function somescript() {
            window.location.href = document.getElementById('ololo').href;
    }
</script>

<a href="javascript:alert('test');" id="ololo">test</a>
<br />

<a href="javascript:somescript()">click me</a>

and, while it works on a local page even when using onload, it doesn't work when I use it in my script.

Probably I'm missing something when transferring the code from the body of an html page to a Greasemonkey script.

I hope this time the question is more clear, excuse me for any misunderstanding, but I'm still a beginner with JS.

A: 

Will this work for your scenario?

<script type="text/javascript">
  function somescript() {
    document.getElementById('ololo').click();//fake a click on the link
  }
</script>
scunliffe
This won't work in Firefox.
SLaks
It doesn't work.Is it possible that a website forbids this kind of actions?
Gurzo
Ah, nicely noted... this used to work (and still does in IE)
scunliffe
The jQuery library provides a cross browser function to "click" a link/button, so it can work. How they do it, should be seen in their source code though...
Mike Gleason jr Couturier
This might work if a jQuery click Event is bound to this element.
Alex
+1  A: 
<script type="text/javascript">
    function somescript() {
     eval(document.getElementById('ololo').getAttribute('href').replace('javascript:', ''));
    }
</script>

I can see the alert box..

Please note that this will only work when its javascript code into the href attribute...

Mike Gleason jr Couturier
If you want to fake a click on the link instead, there's an example here... it seems like a lot of work without a 3rd party library: http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/27e7c70e51ff8a99/98cea9cdf065a524%2398cea9cdf065a524?pli=1
Mike Gleason jr Couturier
+1  A: 

Solved it like this:

window.location=document.getElementById('foo').href;

Thanks everyone for answering anyway.

Gurzo