简体   繁体   中英

JavaScript function call not triggering

Can anyone help in this one?

I have a simple link:

<a href="javascript:ShareOnFacebook()" title="Share on Facebook"
   class="btn btn-facebook">
    <i class="fa fa-facebook"></i> Facebook
</a>

and a simple function call:

function ShareOnFacebook() {
    alert("shared");
}

And the function is just not called. I really don't know what is going on. There must be a solution but I'm not seeing it.

The debugger says:

Uncaught ReferenceError: ShareOnFacebook is not defined

UPDATE

The method is called correctly when the page is loaded for the first time. Every time I reload the page the method is not being called.

The following seems to work fine. Just make sure you define the function in a way that your HTML can see it. For example, before you use it in the HTML.

 function ShareOnFacebook() { alert('hello') } 
 <a href="javascript:ShareOnFacebook()" title="Share on Facebook" class="btn btn-facebook"> <i class="fa fa-facebook"></i> Facebook </a> 

You use the following script instead of your html.

<a onclick="ShareOnFacebook()" title="Share on Facebook" class="btn btn-facebook">
    <i class="fa fa-facebook"></i> Facebook
</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM