Skip to main content

Disable Blogger Generated Anchors with jQuery

Sometimes, I want to make another actions using anchor(<a>) tag, for example, design sum menus and showing floating popup. For doing that, I do not define 'href' attribute on the tag and define actions using JavaScript or jQuery like this.

<script>
$(document).ready(function() {
$('#myid').click(function() {
showPopup(..);
});
);
</script>

<a class="class-name" id="myid"> My Text</a>

how to disable default action on anchor tag

It's working well. But a problem occurs when I edit blog post in Compose and HTML mode alternately. When changing the mode from Compose to Edit, the Blogger editing system generates some tags if it is necessary. For example, <br> tags are generated for new lines if edition option for new line is set to use <br>.

For the anchor tag, Blogger generates 'href' attribute with some strange URL with the blogger ID. Current page is reloaded when the link is clicked after user defied action is performed. In upper example, a floating popup is shown in a second and the page is refreshed.

I can modify the code in HTML mode on HTML editor, but sometime it is forgotten and if many pages have same problem, it is a terrible work.

My solution is to remove default actions (i.e. move to destination URL) of anchor tags. We can use jQuery method preventDefault() on Event object. If the method preventEvent() is called, the default action of the event will not be triggered. Now upper codes can be modified as follows. If you want to prevent events on all anchor tags, you can just replace '#myid' to 'a' on JavaScript codes.
<script>
$(document).ready(function() {
$('#myid').click(function(e) {
e.preventDefault();
showPopup(..);
});
);
</script>

<a class="class-name" id="myid"> My Text</a>



Comments

Post a Comment

Popular posts from this blog

Google's Favorite Colors By Hex Codes (With New Google Logo)

Google keeps its color scheme cross their products. Of course, each uses different colors but it keeps the main philosophy. I heard that the test period for new Google logo was more than one year. It may the reason that the colors are comfortable to look. Sometimes, I copy the colors for designing my blogs and web sites. Copyright? oh..

Color Conversion: RGB to HEX , HEX to RGB

Color conversion between RGB and Hex is needed sometimes even though HTML and CSS accepts both RGB and HEX codes for set color scheme of HTML elements. The conversion algorithm is simple. Then numbers in RGB format is decimal number and one in HEX code is hexadecimal number. For example, rgb(50, 100, 250) is same in HEX #3264FA: 50-32 | 100-64 | 250-FA.

Why Blogger Mobile Template Not Work?

Google Blogger shows blog pages with mobile CSS on mobile devices unless you change it disabled. You can see next figure by clicking mobile setting button on Template page. Since I want to change CSS and I remember there are some CSS elements starting with '.mobile', I open the template editor and edit as I want. And open my blog in my smart phone. Nothing changed.