tags:

views:

390

answers:

3

I need to execute a certain JavaScript when accesing the custom URL.

At this time the code behind the button that triggers the JavaScript that I need to be ran is this:

  <a class="button" href="#" 

  onclick="new Request.HTML({
  method: 'post',
  url: '/ro/somefolder/anotherfolder',
  data: {'user_id': 777, 'score': 1, 'redirect': '1'},
  update: $('vote_user_777'),
  evalScripts: true
}).send();return false;">

<img src="/images/game/thumbsup.gif" alt="vote">
</a>

</span>
</p>


A: 

If you're referring to embedding your JavaScript in a URL rather than attaching it as an event handler, you could do something like this:

<a href="javascript:void(new Request.HTML({...}).send())">

Using the void() function ensures that nothing is returned so that the browser does not navigate away from the current page.

Ben Blank
The return false statement in his function already takes care of that.
Andrew Noyes
Yes, but if he's trying to embed his script in a URL, returning false will either generate an error or take you to a page named "false". When using a `javascript:` URL, you should wrap it in `void()` instead.
Ben Blank
Actually, when the onclick handler returns false, it doesn't execute it's default, which is traveling to the href.
seanmonstar
Yes… I realize that. You'll note that the example code I provided doesn't *have* an onclick handler. This is *instead of* an onclick handler, as stated in my answer.
Ben Blank
+1  A: 

You didn't say what problem you were experiencing, but the syntax looks like the MooTools library.

So I put together a little Javascript that should work, and is more in line with MooTools style.

  window.addEvent('domready',function() {
    var request = new Request.HTML({
     method: 'post',
     url: '/ro/somefolder/anotherfolder',
     data: {'user_id': 777, 'score': 1, 'redirect': '1'},
     update: $('vote_user_777'),
     evalScripts: true
    });
    $$('a.button').addEvent('click',function(e) {
     e.stop();
     request.send();
    });
});
seanmonstar
A: 

No one ? Please check my comment attached to the question. That should clear things up. Thanks.

Cumatru