jeremyok
jeremyok

Reputation: 121

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

Upvotes: 7

Views: 10196

Answers (3)

Finesse
Finesse

Reputation: 10801

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.

Upvotes: 17

msantos
msantos

Reputation: 791

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>

Upvotes: 4

Heretic Monkey
Heretic Monkey

Reputation: 12112

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

Upvotes: 0

Related Questions