3

I want to execute a href without redirecting me to the page. execution in background. here is my code

    <a id="speechOutputLink" href="http://translate.google.com/translate_tts?tl=fr&q=text" rel="noreferrer" >Download</a>

When i click on the the href Download i don't want to be redirected to the page of google translator. How can i do this?

junior developper
  • 448
  • 2
  • 19
  • 40
  • 2
    What exactly do you want if you don't want to be redirected? Do you need to do some javascript process instead of going to the url or you want it to open in another tab or window? – andrex Sep 22 '14 at 14:27
  • The href i'm executing redirect me to the google translator audio playin the text in the href. What i want is to get the audio charged without redirecting me to the google translator page – junior developper Sep 22 '14 at 14:55

2 Answers2

2

Use jQuery's event.preventDefault() method:

If this method is called, the default action of the event will not be triggered.

$('a#speechOutputLink').on('click', function(e) {
    e.preventDefault();

    ...
});
James Donnelly
  • 126,410
  • 34
  • 208
  • 218
0

Adding the 'target' tag to your anchor allows you to open the page in the background or in an iframe, etc. The values accepted are

_blank
_parent
_self
_top
framename
<a id="speechOutputLink" href="http://translate.google.com/translate_tts?tl=fr&q=text" rel="noreferrer" target="_blank">Download</a>

Source: http://www.w3schools.com/tags/tag_a.asp

Additionally you could use the jquery method event.preventDefault() although i've found this to not always work as expected depending on other javascript tasks running.

Eric Hyde
  • 94
  • 13