Solved

How to execute javascript on load

Posted on 2014-01-05
12
335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
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 …
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

734 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