0

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

Taplar
  • 24,788
  • 4
  • 22
  • 35
Luca Belletti
  • 103
  • 12
  • Does that url actually do anything? You can get the current url in jquery like this `var url = $(location).attr('href');` – chevybow May 30 '18 at 14:23
  • Why would you even use jQuery to do that? `location.href` works just fine. Just because jQuery is helpful in many cases, doesn't mean it has to **always** be used. – Taplar May 30 '18 at 14:24
  • I have to insert the field in the contact form,** I do not know if it is the best way** but I have to be able to say that "http://www.xxxx.com/xxxxxxxxx/?adv_source=AdWords" has only this inside "? adv_source = AdWords "and then turn it into" Adwords "instead if it is not so, it must return" seo ". – Luca Belletti May 30 '18 at 14:33

2 Answers2

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.

executable
  • 3,365
  • 6
  • 24
  • 52
0

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);
chevybow
  • 9,959
  • 6
  • 24
  • 39