最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - a href=javascript:function() in firefox not working - Stack Overflow

programmeradmin2浏览0评论

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("", '_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

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

Share Improve this question edited Aug 9, 2016 at 23:59 jeremyok asked Aug 9, 2016 at 17:16 jeremyokjeremyok 1211 gold badge2 silver badges8 bronze badges 7
  • 1 Why not use the onclick instead of the href? This method also has some issues with ie9 – Isabel Inc Commented Aug 9, 2016 at 17:27
  • Did you try all-lowercase javascript:? – user663031 Commented Aug 9, 2016 at 18:29
  • 1 Don't mix your JavaScript with your HTML. <a id="verify" ... <script>window.onload = function () { document.getElementById('verify').addEventListener('click', test); };</script> – Heretic Monkey Commented Aug 9, 2016 at 19:09
  • Hi siabel, I should have add that im using live editor and theres no custom field i could use to add the onclick function if i want to use their button though. – jeremyok Commented Aug 9, 2016 at 23:41
  • Hi torazaburo, i will try all lower case later and update. – jeremyok Commented Aug 9, 2016 at 23:43
 |  Show 2 more comments

3 Answers 3

Reset to default 17

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.

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>

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

发布评论

评论列表(0)

  1. 暂无评论