skip to Main Content

I do not know a lot about jquery, but I’m looking for a solution for this problem.
I need to extrapolate the last parameter of the URL, if it is equal to “adv_source = AdWords” then I must return “AdWords”, otherwise I must give “SEO”.
This is what I managed to do until now.
Can anyone help me?

var url=jQuery('[_url]').val(); 

if (url.search("adv_source=AdWords")) seoadwords = "Adwords";
else seoadwords = "SEO";

Thanks in advance

2

Answers


  1. Here is a working solution :

       let searchParams = new URLSearchParams(window.location.search) //get URL
       searchParams.has('adv_source'); //Search the parameter adv_source
       let param = searchParams.get('adv_source'); //search the value
       if(param == "AdWords"){
          seoadwords = "Adwords";
       }else{
          seoadwords = "SEO";
       }
    

    I found the solution here : Get url parameter jquery Or How to Get Query String Values In js

    Or shorter solution:

    var parameter = (location.search.split('adv_source' + '=')[1] || '').split('&')[0];
    if(parameter == "AdWords"){
        seoadwords = "Adwords";
    }else{
        seoadwords = "SEO";
    }
    

    I hope it helps.

    Login or Signup to reply.
  2. Without knowing the format of your url’s you can do

    var url = $(location).attr('href');
    if (url.contains('adv_source=AdWords')) {
        var seowords = 'Adwords';
    }
    else {
      var seowords = 'SEO';
    }
    
    console.log(seowords);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search