What is the best way to disregard additional parameters in a URL?

< newbie > I've come across an interesting issue with my code. It works perfectly when I use the correct URL:

www.exemple.com/?login

$(document).ready(function(){

function urlparam(){
    var url = window.location.href.split('?')[1];
    if (url == 'login' ) {
        document.getElementById('loginMainContainer').style.display = 'block';
        $('.login').triggerHandler('click');
    } else if (url == 'signup' ){
            document.getElementById('loginMainContainer').style.display = 'block';
            $('.signup').triggerHandler('click');
    }
}
    urlparam()   
});

However, when I try to use a URL with additional parameters like (w/ mailchimp):

www.exemple.com/?login&utm_source=App+Accounts&utm_campaign=e94f95d4da-News_promo+email_English_2016

My function doesn't seem to execute properly. Is there a way for me to ignore these extra parameters and still have my function work correctly?

Thanks in advance for any assistance!

< /newbie >

Answer №1

To solve this problem, I recommend using a regular expression to check if the string starts with either "login" or "signup".

    var url = ('www.example.com/?login&utm_source=App+Accounts&utm_campaign=e94f95d4da-News_promo+email_English_2016').toString().split('?')[1];
    if (url.match(/^login/ )) {
        console.log('login');
    } else if (url.match(/^signup/ )){
        console.log('signup');
    }

Answer №2

To determine if the parameter 'login' exists, you need to collect all parameters and then check for the presence of 'login'.

var url="http://someurl.com?test=ok&machin=1234&login&azir=lol";

function checkParameter(url, param) {
  var params = url.split('?')[1].split('&');
  for(var j = 0; j < params.length; j++) {
     var paramNameURL = params[j].split('=')[0];
     if(paramNameURL == param)
       return true;
  }
  return false;
}

console.log(checkParameter(url, "login"));
console.log(checkParameter(url, "example"));

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

Incorrect horizontal scrolling within a jQuery div container

I have implemented a plugin for horizontal scrolling on my website. You can check out the plugin here. Here is the demo link showcasing the plugin in action: However, I am encountering an issue where the pages do not scroll when the link to scroll is pla ...

When a new element is added to the DOM, bind a click event to it that will trigger another

When I add an element to the DOM, I bind it with a click function. The problem is that if I add multiple elements and click on any of them, the function triggers multiple times. What causes this behavior and is there a better way to achieve the desired res ...

React - Strategies for handling an empty array in .map() operations

Struggling with rendering an empty list in React and JavaScript, specifically for search results. The goal is to display search results after the user clicks on the search button. However, the list remains empty and does not update even after retrieving ...

Step-by-step guide on permanently assigning a value in Node.js

I recently started working with node js and I am facing an issue where I need to save an id in a global variable that is required in multiple functions. However, every time the server restarts, the variable gets emptied. Is there a way to persist this va ...

Vue - the <script setup> element is unable to execute scripts

I have a functional widget from Atlassian that works as expected when used in an HTML file: <html lang="en"> <head> <script data-jsd-embedded data-key=<key> data-base-url=https://jsd-widget.atlassian.com src=https://jsd-w ...

The query limit issue in Sails JS

I am encountering a 413 (Request Entity too large) error when making a request to a Sails Js app (v0.12). Despite my attempts to raise the request limit in the bodyParser, I have not seen any changes take effect. In config/http.js, I included a customize ...

Encountering an error when attempting to upload a file to S3 using JS Vue

I'm attempting to upload a file to my S3 bucket using the aws-s3 library, but I am encountering this error in the console: https://i.stack.imgur.com/lk47U.png Here is the code for the component: <template> <input type="file" @ch ...

Encountering a prop validation error in JSX when using an onClick event and passing a function that is not recognized as

Struggling with prop validation issue where I am encountering an error while passing a function as a prop and the error message claims it is not a function. Looking at Contact.jsx: import Avatar from "./Avatar"; import PropTypes from "prop ...

Substitute any text string starting with a colon, such as the route path in Express

Below are some sample strings: const a = '/example/:someItemUuid/hello' const b = '/example/:someItemUuid/hello/:otherItemUuid' const params = { someItemUuid: '12345', otherItemUuid: '67890' } I am in search o ...

Node.js client encounters ENOBUFS error due to excessive number of HTTP requests

Currently, I have the following setup: An end-to-end requests system where a node.js client communicates with a node.js server. However, the issue arises when the client fails with an ENOBUFS error in less than a minute. client: (function(){ var lo ...

Designing a comprehensive window evaluator for color selection

Hey everyone, I'm excited to be joining this forum and it's my first time posting. While I have some experience in programming, particularly in HTML, I've recently encountered a new challenge. I'm interested in creating a full window c ...

Refreshing a component in Angular/Angular2 using routerLink from the NavBar when already on the current route

When I am on the same route and click again from the navbar, nothing happens. Is there a way to refresh my component directly from the navbar using a method in routerLink? <li [routerLinkActive]="['active']"><a [routerLink]="['/ca ...

Issue with Cordova: Onsen UI failing to run scripts

Currently, I am utilizing Cordova along with pure JavaScript. My goal is to have the ons.ready() function execute from a JavaScript file named ui.js, however, it seems to be ineffective. The code snippet in question appears as follows: document.addEve ...

Angular logs the HTTP error before it is processed

When a user clicks on a button on a simple login form, a mechanism is triggered. This mechanism involves sending a POST request to a REST service with the user's provided credentials. If the credentials are correct, the success function is executed, o ...

What is the best way to remove an object from an array in MongoDB?

I am currently using MongoDB and Node.js, but I am facing a small issue when trying to delete an object from an array of objects. Below is the code snippet causing the problem: router.route('/deleteGuestFromJam/:id').delete(function(req, res){ ...

React object mapping issue: key causing update problem

I am encountering a fundamental issue with updating my data via an AJAX request and then setting the state of that data. However, despite updating the state, the view does not reflect these changes. Upon investigation, it seems this discrepancy arises beca ...

Leveraging the power of PHP, Ajax, and JavaScript to dynamically populate individual div containers with data from each row of my database

I am working with a database table containing an id,name, and city. My goal is to display the name and city in a div for each row of the table. Here's my HTML code: <html> <head> <script> function ajax_post(){ var ...

Sort the numbers in the table from greatest to least (ReactJS)

I am struggling to create a table that is paginated, but the numbering does not reset when a new page begins. Instead, it should continue sequentially from the start to the end. For instance, on the first page: 10 XXX 9 XXX 8 XXX 7 XXX 6 XXX And then on ...

Dynamically generated input fields using jQuery

Here is the code I'm using to dynamically add and remove input fields: I've encountered two issues that I haven't been able to resolve: $(wrapper).append('<div><input type="text" name="mytext[]"/><a href="#" class="remo ...

It is impossible for two long-lasting PHP scripts to run concurrently on a server

I have a PHP script that runs for an extended period of time on the server. This script streams video content to the video tag, for reasons unknown (don't judge me :-) ). The script can run for tens of minutes. The issue arises when I attempt to make ...