0

I am getting the error PriceSelling is not defined. But it is, infact I know that it is on the page because it logs it in the console. Please Help! Thanks

$.get(window.location, function(data){
   var regex=/<span class="it " data-se="item-privatesale-price">([\d,]+)<\/span>/;
   var PriceSelling = data.match(regex)[1];  
    console.log(PriceSelling);
});

function get(name){
   if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search))
      return decodeURIComponent(name[1]);
} 

if (get('bot') && get('expecting') && get('expecting') == PriceSelling) {
console.log("It's a go!");
document.getElementsByClassName('conf-buy-now btn-primary btn-medium PurchaseButton ')[0].click();
//document.getElementById('conf-confirm-btn').click();
}; 
RobG
  • 142,382
  • 31
  • 172
  • 209

1 Answers1

0

It is defined in the scope of the callback function passed to $.get.

But it is not defined in the global scope.

You could do

var PriceSelling;

$.get(window.location, function(data){
  var regex=/<span class="it " data-se="item-privatesale-price">([\d,]+)<\/span>/;
  PriceSelling = data.match(regex)[1];  
  console.log(PriceSelling);
});

function get(name){
  if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search))
    return decodeURIComponent(name[1]);
} 

if (get('bot') && get('expecting') && get('expecting') == PriceSelling) {
  console.log("It's a go!");
  document.getElementsByClassName('conf-buy-now btn-primary btn-medium PurchaseButton ')[0].click();
  //document.getElementById('conf-confirm-btn').click();
} 

but while you wouldn't get a ReferenceError, it wouldn't be much good since PriceSelling would always be undefined.

But I notice that you are trying to use the response right away. You have to use it in the callback, which is called once the response has been received.

You might benefit from How do I return the response from an asynchronous call?.

$.get(window.location, function(data){
  var regex=/<span class="it " data-se="item-privatesale-price">([\d,]+)<\/span>/;
  var PriceSelling = data.match(regex)[1];  
  console.log(PriceSelling);

  function get(name){
    if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search))
      return decodeURIComponent(name[1]);
  } 

  if (get('bot') && get('expecting') && get('expecting') == PriceSelling) {
    console.log("It's a go!");
    document.getElementsByClassName('conf-buy-now btn-primary btn-medium PurchaseButton ')[0].click();
    //document.getElementById('conf-confirm-btn').click();
  } 
});
Community
  • 1
  • 1
Paul Draper
  • 78,542
  • 46
  • 206
  • 285