Solved

How to execute javascript on load

Posted on 2014-01-05
12
333 Views
Last Modified: 2014-01-06
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 ?
0
Comment
Question by:team2005
  • 6
  • 4
  • 2
12 Comments
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 150 total points
ID: 39757970
Do you want to pass a hardcoded value for organisasjon? And you still want have the same code for combobox change event? If so you have to duplicate the code this way:

<script type="text/javascript">

$(document).ready(function() {
//this code is executed when the page is loaded
  $.ajax({
    url : 'selektbrukere.php',
    type : 'POST',
    dataType: 'json',
    data : { organisasjon : HARDCODED VALUE },
    success : function (data) {
      $("#brukere").html(data.text1);
      $("#kontrolln load is er").html(data.text2);
    }

  });

//and this one when combobox changes1
$('#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

0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 39758207
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

0
 
LVL 2

Author Comment

by:team2005
ID: 39758620
Hi!

Okey, that works fine...
Is it possible to select the default value from PHP variable ?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:team2005
ID: 39758642
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 ?
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39758653
Sorry, but are you referring to my answer or to tommyBoy's one?
0
 
LVL 2

Author Comment

by:team2005
ID: 39758700
Hi!

You and tommy...
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39758899
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

0
 
LVL 2

Author Comment

by:team2005
ID: 39759008
Hi!

Default values works fine, but not when organisation is changed ?
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39759018
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.
0
 
LVL 2

Author Comment

by:team2005
ID: 39759035
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

0
 
LVL 38

Accepted Solution

by:
Tom Beck earned 350 total points
ID: 39759042
Don't put the document ready inside the window load function. Also, the onChange bind event handler should not return false.
<script>
$(window).on('load', function(){
	runAjax('hardcodedValue');
});
$(document).ready(function() {	
	$('#organisasjon').bind("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

0
 
LVL 2

Author Closing Comment

by:team2005
ID: 39759152
Working great now, thanks :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Session on Html 8 37
How do use '  ' within this code? 4 24
Difference between PHPClasse.php and SimplXLS.php 5 16
Check input text, Number 6 27
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question