Link to home
Start Free TrialLog in
Avatar of team2005
team2005

asked on

How to execute javascript on load

Hi!

Have this working javascript, that are called when i select a value from the
combobox -> organisasjon...
<script type="text/javascript">

$(document).ready(function() {
$('#organisasjon').change(function() {
  $.ajax({
    url : 'selektbrukere.php',
    type : 'POST',
    dataType: 'json',
    data : { organisasjon : $(this).val() },
    success : function (data) {
      $("#brukere").html(data.text1);
      $("#kontrolln load is er").html(data.text2);
    }

  });
});
});

</script>

Open in new window


But i want this to be called, when the page is loaded to...

How can i do that ?
SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Best to modularize the ajax call.
<script>
$(window).on('load', function(){
	runAjax('hardcodedValue');
});

$('#organisasjon').change(function() {
	runAjax($(this).val());
});

function runAjax(val) {
	$.ajax({
    url : 'selektbrukere.php',
    type : 'POST',
    dataType: 'json',
    data : { organisasjon : val },
    success : function (data) {
      $("#brukere").html(data.text1);
      $("#kontrolln load is er").html(data.text2);
    }

  });
}
</script>

Open in new window

Avatar of team2005
team2005

ASKER

Hi!

Okey, that works fine...
Is it possible to select the default value from PHP variable ?
Hi!

I was mistaken, it dosent works..

When page is loaded, it shows data correct (page loaded)
But when i change organisation combo... it dosent change ?
Sorry, but are you referring to my answer or to tommyBoy's one?
Hi!

You and tommy...
Try using this slightly modified code by tommyBoy. I used jquery bind() function which should act as standard javascript addEventListener function:

<script>
$(window).on('load', function(){
	runAjax('hardcodedValue');
});

$('#organisasjon').bind("change", (function() {
	runAjax($(this).val());
}), false);

function runAjax(val) {
	$.ajax({
    url : 'selektbrukere.php',
    type : 'POST',
    dataType: 'json',
    data : { organisasjon : val },
    success : function (data) {
      $("#brukere").html(data.text1);
      $("#kontrolln load is er").html(data.text2);
    }

  });
}
</script>
                                            

Open in new window

Hi!

Default values works fine, but not when organisation is changed ?
Mmmhh, perhaps we forgot to enclose all code within (document).ready as you did in original snippet:

$(document).ready(function() {

});

Open in new window


Secondly, I would add a little alert in the change event:

$('#organisasjon').change(function() {
        alert('change event!');
	runAjax($(this).val());
});

Open in new window


So we can see if it is raised and the problem resides in another place.
Hi!

Still dosent work, dosent alert....

<script type="text/javascript">

$(document).ready(function() {
$(window).on('load', function(){
  runAjax('Softkey');
});


$('#organisasjon').bind("change", (function() {
  alert('change event!');
  runAjax($(this).val());
}), false);

function runAjax(val) {
  $.ajax({
    url : 'selektbrukere.php',
    type : 'POST',
    dataType: 'json',
    data : { organisasjon : val },
    success : function (data) {
      $("#brukere").html(data.text1);
      $("#kontroller").html(data.text2);
    }

  });
  
}
});

</script>

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Working great now, thanks :)