What is the reason for the click event in a Chrome extension's popup to refresh the popup's

While exploring Chrome extensions, I came across a strange issue with the popup window.

Inside popup.html, there is an element

<a id="grantexpert">...</a>
and in popup.js, I want to change the background color of this element when clicked. Initially, changing the color immediately after document ready works fine. However, during the click event, the color changes briefly (just a blink) and then reverts back to its original state. It seems like the popup window is being reloaded after each click event.

Manifest:

  "browser_action": {
    "default_popup": "popup/popup.html"
  },

popup.html:

<!DOCTYPE html>
  <head>
    <script src='../other/jquery-3.1.1.min.js'></script>
    <script src='popup.js'></script>
  </head>

  <body style="width:250px;">

    <a class="testitem" href id="grantexpert">grantexpert.sk</a>

  </body>
</html>

popup.js:

(function($) {

  $(document).ready(function() {

    //$("#grantexpert").css('font-size', '28px'); - this works

    // this does not work
    $('#grantexpert').click(function () {
      $(this).css('font-size', '28px');
    });

  });

})(jQuery);

Why does the element change color momentarily on click but fail to retain the modified properties afterwards?

Answer №1

Consider removing the empty href attribute from the a element:

<a class="samplelink" href id="webmaster">webmaster.com</a>

and change it to:

<a class="samplelink" id="webmaster">webmaster.com</a>

Answer №2

My experience was quite similar to this. Whenever I added a simple button to the popup, such as:

<button id="myCustomButton">Click Me</button>

the act of clicking on it resulted in the popup being refreshed. To prevent this unwanted behavior, it is necessary to stop the default event from happening. This can be achieved using jQuery, like so:

$("#myCustomButton").click(function(event){
  event.preventDefault(); // Prevents the popup from refreshing
  // Perform some action here
  displayMessage("Button has been clicked");
});

Answer №3

Eliminate the href attribute from the tag to prevent the popup from refreshing when clicked. This will maintain the current URL and provide a smoother user experience.

Answer №4

Clicking the link will trigger a page reload.

<a href ... 

Consider adding a NOP instruction to the link:

<a href="javascript:;" ... 

Alternatively, you can remove it as others have suggested:

<a ...

If you're interested in a similar topic, check out these related questions: Is using an empty href attribute valid? and What is the best "href" value for JavaScript links, "#", or "javascript:void(0)"?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide on populating a dropdown menu with values based on the selection of another dropdown menu

Could you please help with a situation involving two dropdown lists, one for country and one for state? I am trying to load states based on the chosen country value. I have set up an AJAX call and triggered the change event for the country dropdown. My m ...

Animating all the numbers on a jQuery countdown timer

http://jsfiddle.net/GNrUM/ The linked jsfiddle showcases a countdown of 2 seconds, with only the numbers displayed. My goal was to explore: a) How can I incorporate animations into the JavaScript code so that the number changes in size, color, and positi ...

Show custom value when hovering over MySQL table row

I have a webpage that showcases a table with data fetched from a MySQL database. Each value is depicted by an icon, and upon clicking the icon, a popup window displays the specific value...I am interested in having the value appear in an info box near the ...

Exploring Django's AJAX suggestions

I successfully created a functional website using Django, but I am facing a challenge with two separate forms that are currently on different pages. Since these forms are central to the web application's activities, I would like to combine them onto t ...

Execute `preventDefault` prior to authenticating the User with Dev

In my index, I have a modal with a user sign-up form. I created a JavaScript function that triggers when the "Save" button is clicked. If users encounter errors, alerts appear in the modal but it redirects to another page. I want the page to stay on the c ...

Implementing a Variety of Textures and Images in a Three.js Scene

I am utilizing THREE.js to showcase a 3D spinning globe in the web browser. Along with that, I intend for an image to display around the rotating globe. Despite attempting to use the provided function, var img = new THREE.ImageLoader(); img.load("texture/ ...

Update Jquery Dialog's Button After Making an Ajax Request

I am facing an issue with my dialog window that contains some confirmation elements following a form submission: $('#newUserDialog').dialog({ autoOpen: false, width: 600, modal: true, resizable: false, cl ...

Is there a way to automatically convert a webpage to a PDF when it is requested?

I'm looking to convert the webpage into a PDF file upon request. Below are the scripts I am using: <script> pdfdoc.fromHTML($('#container').html(), 10, 10, { 'width': 110, ...

The Bootstrap v5 dropdown feature is malfunctioning as the drop-down menu fails to appear

I've been struggling to create a dropdown menu using Bootstrap, but it doesn't seem to be working as expected. Despite that, I have no issues utilizing the framework for styling purposes. The Bootstrap js link is placed at the bottom of the body ...

Dynamically extracting JSON values

This question may seem very basic to some, but I am struggling with a specific part in the code snippet below. I am trying to make the 'res.DATA.VALUE[i]' part dynamic by using the 'textCol' variable. My intention is to replace it with ...

The flash movie covers part of the menu that is relatively positioned

While designing a website, I encountered an issue with the Flash slider (CU3ER) overlapping the menu bar. Despite trying various z-index configurations to ensure the drop-down menu stays on top, none of them seem to work. Initially, the drop-down menu rem ...

Is it possible to set a value for a jQuery function after the function has

I am a beginner when it comes to javascript & jQuery. I am currently working on a feature for my website where users can display badges they have earned on their own site by simply copying and pasting a bit of code that I provide. Although the javascript p ...

Learn how to retrieve data using the $.ajax() function in jQuery and effectively showcase it on your HTML page

Can someone assist me with extracting data from https://jsonplaceholder.typicode.com/? Below is the AJAX call I'm using: $.ajax({ url: root + '/posts/', data: { userId: 1 }, type: "GET", dataType: "json", success: function(data) { ...

Using Jquery in combination with Bootstrap modal and PHP for website

Upon submission of a form with numerous fields, I aim to display a bootstrap modal popup that prompts the user with a question before data is saved in the database. The user can respond with either "yes" or "no", leading to different actions being taken ba ...

Is there a way to change the font size with a click in JavaScript or Angular?

Here is a breakdown of the 4 steps: 1.) Begin by clicking on a category 2.) The filtered products will be displayed 3.) Select the desired products from the filter 4.) Once selected, the products will appear in the rightmost part of the screen within t ...

Can you explain the distinction between these two jQuery selectors?

Is the result of $('#e > #f') the same as $('#e #f') when using this markup: <div id="e"> <div id="f"></div> </div> ...

What is the procedure for submitting all checkbox values through Google Apps Script?

My web app created using Google Apps Script can generate a custom table of data for users to select for calculations. I use Google Sheets to populate the table with values and checkboxes, but note that the table size may vary depending on the user. <fo ...

I am interested in incorporating a vertical scrollbar into a gridview in ASP.NET

I've successfully bound data to a grid view in my ASP.NET application. I've implemented pagination, but instead of that, I'd like to incorporate a scroll bar to prevent lengthy paging. Below is the code for my grid view: <div class="box ...

Combining JQuery with ASP.NET Web Forms to create dynamic and interactive web

I have embarked on creating an ASP.NET Web Form application that aims to incorporate some cool jQuery features and visual appeal. One part of my application includes two jQuery UI tabs: a search tab and a results tab. Upon conducting a search from the sear ...

Tips for adjusting the color of a row when hovering

Can I modify the row color on hover in material-table using props? Similar to this example. Appreciate your help. ...