The JavaScript alert message pops up two times consecutively

I encountered an issue while attempting to utilize a module named disclaimer in Drupal 7. The alert message "You must enter the year you were born in." appears twice and then redirects to a URL that should only be accessed after verifying that you are over 18 years old.

Despite trying suggested solutions, the issue persists with the alert showing up twice. I suspect the problem may lie in the action of the enter button. Below is the function for it.

 Drupal.behaviors.disclaimer = {
  attach: function (context, settings) {
    // action on enter button
    $('#disclaimer_enter', context).click(function () {
      var check = true;
      // check if age form is set
      if (settings.disclaimer.ageform === 1) {
        var check = Drupal.checkAge();
      }
      // if everything is fine, add cookie and close colorbox
      if (check) {
        $.cookie(settings.disclaimer.cookie_name, '1', { path: settings.disclaimer.cookie_path });
        $.colorbox.remove();
      }
    });
  },
};

}

Drupal.checkAge = function () {
    var now = new Date();
    var date = now.getDate();
    var month = now.getMonth() + 1;
    var year = now.getFullYear();
    var optmonth = jQuery("#edit-disclaimer-age-month option:selected").val();
    var optday = jQuery("#edit-disclaimer-age-day option:selected").val();
    var optyear = jQuery("#edit-disclaimer-age-year option:selected").val();
    var age = year - optyear;
    if (optmonth > month) {age--;} else {if(optmonth == month && optday >= date) {age--;}}
    // if current year, form not set
    if (optyear == year) {
      alert(Drupal.t("You must enter the year you were born in."));
      return alert;
    // if under age, alert and redirect !
    } else if (age < Drupal.settings.disclaimer.limit) {
      alert(Drupal.t("Sorry, you are Under age limit and are prohibited from entering this site!"));
      location.replace(Drupal.settings.disclaimer.exiturl);
      return false;
    } else {
      // age limit ok
      return true;
    }
  }

Answer №1

When you use the alert function, it will display the alert message twice. Additionally, if you do not include a false return statement, the script will proceed to the next page.

Here is an alternative approach:

if (optyear == year) {
    alert(Drupal.t("You must enter the year you were born in."));
    return false;
}

Answer №2

It is not essential to use the return alert.

alert(Drupal.t("You must provide your birth year."));
//return alert;

The alert will be triggered simply by using alert();

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

Using an object as a parameter in a NodeJS function

This past week, I encountered a challenge that has been difficult to overcome. I've been attempting to pass a JSON object as a parameter in a function, but I keep receiving an error message stating that it's not possible. Unfortunately, I don&apo ...

Turn off and then reinstall Image when clicked

function show(){ alert("i am pixel"); } function disableImgClick(){ $(".Dicon").unbind('click'); } $(document).ready(function(){ $("#turnoff_btn").click(function (e){ e.preventDefault(); disableImgClick(); }); }); i have a group of ima ...

What causes the difference in behavior between using setInterval() with a named function as an argument versus using an anonymous function?

I can't seem to figure out why using the function name in setInterval is causing issues, while passing an anonymous function works perfectly fine. In the example that's not working (it's logging NaN to the console and before the first call, ...

Troubleshooting: Why the TableTools basic usage example is not functioning as

After replicating the code from http://datatables.net/release-datatables/extensions/TableTools/examples/simple.html in my Visual Studio project, I organized the files by saving two css files as style1.css and style2.css, along with three js files named sc ...

Setting up eslint for your new react project---Would you like any further

I am currently working on a TypeScript-based React application. To start off, I used the following command to create my React app with TypeScript template: npx create-react-app test-app --template typescript It's worth noting that eslint comes pre-co ...

Discovering the method for accessing nested JSON data through ng-repeat in AngularJS

I'm attempting to access the items._id value within ng-view using ng-repeat. I am able to retrieve all data, but I am interested in specific data only. Data.json [ { _id : "td6v9db4514cc4ewew4334", firstName : 'ayaz', la ...

Automatically, the "ng-hide" class gets added despite using the correct syntax for ng-show

I am trying to dynamically show/hide a tr tag within a table based on the value of a scope variable in the controller. Despite writing the code correctly, I am facing an issue where the "ng-hide" class is automatically added to the tr tag every time it is ...

A guide on displaying an array object in a table column using Vue.js and Element UI

Having issues with rendering array data in Vue Js Element UI using the el-table-column element. The string data displays correctly, but I'm facing problems with the array data. I've attempted to include static data within the data() return method ...

Utilizing AJAX to amplify the worth of plupload's echo capabilities

Greetings! Here is the JavaScript code I am using for plupload var uploader = new plupload.Uploader({ runtimes : 'html5,flash,silverlight,html4', browse_button : 'pickfiles', // you can pass in id... container: document.getElementById( ...

What are some ways to direct users from one page to another without relying on server-side programming?

Is there a way to create a redirect page using jQuery or JavaScript? What is the process of writing client-side scripting code to redirect the browser from one page (page1) to another page (page n)? ...

Navigate divs with varying z-index values

I want to change the z-index of 3 divs by toggling them using jQuery, but I'm not sure how to do it. What I want is to click a button that relates to a specific div and increase the z-index of that div so the content inside it is shown. Currently, all ...

Executing PHP include statement with ajax: A step-by-step guide

I am trying to achieve a feature on my website where when a button is clicked in the index.php file, an AJAX request will load about.php and display it inside a div tag within index.php. Unfortunately, my current code doesn't seem to be working as exp ...

Discovering which page the form was submitted from using xsl template

Incorporating code like <input type="hidden" value="contact-form-1" name="getpage"> into my form is something I'm interested in, as it allows me to retrieve the URL of the page from which the form was submitted. The challenge arises because the ...

Remove a row from a Jquery Jtable

I am running into difficulties when trying to delete rows, and I suspect that the issue might be related to the post[id] not being properly sent. Although the delete message appears, the row is not actually deleted. Below is the snippet of my code: JAVASC ...

Steps for displaying a loading gif animation prior to retrieving text content using JavaScript

Before fetching the PHP result, I want a loading GIF image to display within the div section. <!DOCTYPE html> <html> <head> <title> CTR Calculator Tool | Free Click Through Rate Online Calculator </title> & ...

Encountering issues with ASP.NET WebAPI: When using $.ajax, a 404 error occurs, while using $.getJSON results in an Uncaught

Currently, I am developing an ASP.NET web API with a C# project that I am attempting to call from JavaScript. Below is the snippet of my JavaScript code: function LoadGraph() { var file = document.getElementById("file-datas"); if ('files' in fi ...

Utilizing Ajax for validation on Telerik Kendo Grid with Change Event

I am working with a Telerik Kendo Grid that has a column listing medical diagnoses. The list is populated using a method in the controller. My task is to verify whether a newly added diagnosis is already present in the grid, and only allow adding diagnoses ...

Priority is given to strings over numbers

Here's some code I'm working with: <tbody> <tr> <td class="float-left"> <!-- {{selectedTemplat?.modifiedAt | da ...

Unlock the secrets of recovering deleted data from a text area in Kendo UI Angular

Currently working with Kendo UI for Angular, I need to capture deleted content and remove it from another text area within the same component. My project is using Angular-13. I'm stuck on how to accomplish this task. Any suggestions would be greatly ...

Wcf Service does not define Angular JS methods

I am utilizing a WCF Service within an AngularJS application. The WCF Service is functional, and I am attempting to display a list of user records from a SQL database. However, upon running the application, I encountered the following errors: angular.js: ...