Why do you see colons while calling a javascript function in html sometimes?

前端 未结 3 1873
抹茶落季
抹茶落季 2021-02-20 15:12

A lot of the time I see people calling javascript functions using colon (:).

Like onclick=\"javascript:functionname();\"

The same function works wit

3条回答
  •  盖世英雄少女心
    2021-02-20 15:24

    This is not as commonly seen in onclick events as these events already execute javascript. You will may see something like the following quite a bit:

    Link
    

    The reason for such code is that by default an href attribute is trying to change the location or redirect. This tells the browser that the default href action will be javascript that is run. Otherwise, the function will not run and the page will refresh which is quite annoying.

    Page refreshing is a common issue when using javascript like this in an anchor tag since an anchor tags default action is to refresh or load another page. The return false; at the end signals that the default action (i.e. refresh or load) should not fire.

    Hope this helps.

提交回复
热议问题