I
I
Ivan Ivanov2018-05-07 17:21:53
JavaScript
Ivan Ivanov, 2018-05-07 17:21:53

How to execute an ajax request once on page load?

The point is the following. There is an ajax request that hangs with the onClick event on the buttons. Ajax itself filters the elements on the page.

it looks like this

<?$varOnclick = "BX.ajax.insertToNode('$yearRequest', 'comp_".$arParams["AJAX_ID"]."');  return false;";?>

<div class="year-number <?if($arResult['FILTER_PARAMS']['YEAR'] == $arValue['ID']):?>activated<?endif;?>" id="year-<?=$arValue['ID']?>" onclick="<?=$varOnclick ?>"><?=$arValue['VALUE']?></div> // id у одной из кнопок будет year-30


It is necessary to simulate clicking on one of the buttons when loading the page.

checkFirstLoad = false;

if(!checkFirstLoad) {
oneClick();
}

function oneClick(){
  
    $( "#year-30" ).click(); // имитируем нажатие кнопкой мишы на блок
  
  checkFirstLoad = true;
}


naturally, nothing happens because after ajax execution, the checkFirstLoad variable again becomes false and the code is executed again. Please tell me how to win?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TerNik, 2018-05-07
@ZZiliST

Maybe do it like that?

if (!document.body.hasAttribute('alreadyClicked')) {
  document.body.setAttribute('alreadyClicked', 'true');
  $("#year-30").click();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question