Solved

How to execute javascript on load

Posted on 2014-01-05
12
328 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
html input 8 43
jQuery Dialog Autoresize Bug 2 25
Echo Multiple values from multiple records 19 42
HowTo set a global variable in javascript or jQuery 4 22
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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…

828 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