How to execute JavaScript in a url

I need to execute certain JavaScript when accessing a custom url.

At this time, the code behind the button that triggers the JavaScript I need to run is:

 

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

      

0


a source to share


2 answers


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 more in line with the 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();
    });
});

      

+2


a source


If you mean embedding your JavaScript in the url, rather than attaching it as an event handler, you can do something like this:

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

      



Using the function void()

ensures that nothing is returned so that the browser does not move away from the current page.

0


a source







All Articles