Solved

How to execute javascript on load

Posted on 2014-01-05
12
327 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

815 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now