Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a href=javascript:function() in firefox not working

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

like image 887
jeremyok Avatar asked Aug 09 '16 17:08

jeremyok


People also ask

Can we call JavaScript function in href?

In JavaScript, you can call a function or snippet of JavaScript code through the HREF tag of a link. This can be useful because it means that the given JavaScript code is going to automatically run for someone clicking on the link. HREF refers to the “HREF” attribute within an A LINK tag (hyperlink in HTML).

Can we pass function to href?

You can use the href or on-click method to call a JavaScript function from HTML elements.


2 Answers

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.

like image 155
Finesse Avatar answered Oct 14 '22 08:10

Finesse


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>
like image 23
msantos Avatar answered Oct 14 '22 08:10

msantos