7

I tried using a href=javascript:function() in a button, with a function to execute it. It works in Chrome but it doesn't work in Firefox.

Firefox doesn't alert and open blank tab.

Anyone can help me?

<script>
function verifyisbot() {
alert("test."); 
var win = window.open("http://yahoo.com", '_blank');
win.focus();
}
</script>

Below is button code

<div class="frb_textcenter">
<a  target="_blank" class="frb_button frb_round  frb_center frb_fullwidth" href="Javascript:verifyisbot();">
click here
</a>
</div>

Update

I should have added that im using a live editor(profitbuilder) in wordpress to generate the page and button. There is no area for me to insert additional javascript onclick function to the button. So i figure out to use "ahref" blank field in the live editor to input javascript call function to fire up the function.

Is there any way i can make this work through the ahref without using onclick event? Or can i specify onclick function in the ahref field?

Sorry the test() is actually verifybot() function, typo mistake

jeremyok
  • 121
  • 2
  • 8

3 Answers3

15

You can achieve the goal using only the href attribute:

<a href="javascript:void(verifyisbot())" class="frb_button frb_round frb_center frb_fullwidth">
  click here
</a>

It works, because when a browser follows a javascript: URI, it evaluates the code in the URI and then replaces the contents of the page with the returned value, unless the returned value is undefined. The void operator can be used to return undefined.

Finesse
  • 9,793
  • 7
  • 62
  • 92
4

Use onclick event instead of a href=javascript.It works on firefox.See below:

<div class="frb_textcenter">
<a target="_blank" class="frb_button frb_round  frb_center frb_fullwidth" onclick="test()">click here</a></div>

<script>
function test() {
  alert("test."); 
  var win = window.open("http://yahoo.com", '_blank');
  win.focus();
}
</script>

UPDATE 1: You can do it without use javascript.You just add the link in the href attribute.See below:

<a target="_blank" class="frb_button frb_round  frb_center frb_fullwidth" href="http://yahoo.com">click here</a></div>
msantos
  • 691
  • 5
  • 6
  • Hi msantos, im using live editor though and it seems to have no way to tag the button with onclick to it. Any way i could use the ahref field to fire the alert and open new tab? – jeremyok Aug 09 '16 at 23:55
  • Yeap but i would want the alert function too though.. Can it call thru ahref also? – jeremyok Aug 10 '16 at 00:48
  • The only way is handling by javascript.Look the solution of Mike McCaughan. – msantos Aug 10 '16 at 00:57
  • I see, yes i believe mike solution will work. Though the problem with the live editor is that it doesnt have fields for us to input or add custom id to the button also.. Its annoying :/ – jeremyok Aug 10 '16 at 02:27
0

Give serious consideration to separating your JavaScript and your HTML such that the problem goes away. For instance, add an ID to your anchor and add an event handler through script:

<div class="frb_textcenter">
  <a id="verify" target="_blank" class="frb_button frb_round  frb_center frb_fullwidth" href="http://yahoo.com">
    click here
  </a>
</div>

Later...

<script>
function test() {
  alert("test."); 
  var win = window.open("http://yahoo.com", '_blank');
  win.focus();
}
window.onload = function () { 
  document.getElementById('verify').addEventListener('click', test); 
};
</script>

Do note that with the example provided, you don't actually need JavaScript at all. The HTML itself will cause a new window/tab to open with Yahoo! loaded...

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • Hi mike thank u for the code, i believe your code will work with id name, but the issue is that the live editor im using doesnt allow us to input id name or custom html to the button they generate. Which is annoying.. Is there any way to use ahref field that is provided by the live editor to fire up the function or do a alert and open a new tab? If could it will be awesome – jeremyok Aug 10 '16 at 02:29
  • @jeremyok Change the onload to use `document.querySelector('[href="http://yahoo.com"]')` instead of `document.getElementById('verify')`, assuming that `href` is unique to the document. – Heretic Monkey Aug 12 '16 at 14:02