Cumatru
Cumatru

Reputation: 725

How to execute a JavaScript in a URL

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>

Upvotes: 0

Views: 2519

Answers (2)

seanmonstar
seanmonstar

Reputation: 11452

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();
    });
});

Upvotes: 2

Ben Blank
Ben Blank

Reputation: 56634

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.

Upvotes: 0

Related Questions