Smudger
Smudger

Reputation: 10771

execute javascript onload rather than onchange

I have am HTML form in a PHP page. The form has various inputs. One of the inputs has an onchange event:

<input size=10  type=number id=sku1 name=sku1 onchange="showUser(1, this.value);showWhse(1, this.value)">

This calls the following function:

<script type="text/javascript">  
  function showUser(userNumber, str)  
  {  
    if (str=="")  
    {  
      document.getElementById("txtHint" + userNumber).innerHTML="";  
      return;  
    }    
    if (window.XMLHttpRequest)  
    {// code for IE7+, Firefox, Chrome, Opera, Safari  
      xmlhttp=new XMLHttpRequest();  
    }  

    xmlhttp.onreadystatechange=function()  
    {  
      if (xmlhttp.readyState==4 && xmlhttp.status==200)  
      {  
        //document.getElementById("txtHint" + userNumber).innerHTML=xmlhttp.responseText; 
        var responseText = xmlhttp.responseText;  
        var description = responseText.substring(12, responseText.indexOf(",Warehouse:"));  
        var warehouse = responseText.substring(responseText.indexOf(",Warehouse:")+11, responseText.indexOf(",SellingUnits:"));  
        var sellingUnits = responseText.substring(responseText.indexOf(",SellingUnits:")+14);  

        document.getElementById("whse" + userNumber).innerHTML = warehouse;  
    document.getElementById("txtHint" + userNumber).innerHTML = description;  
    document.getElementById("su" + userNumber).innerHTML = sellingUnits;  
  }  
}  
xmlhttp.open("GET","getdata1.php?q="+str,true);  
xmlhttp.send(); 
} 
</script>

This works 100%. if however, the input is populated by a session variable, and the page is refreshed, how can I get the script to execute again onload, without an onchange event?

so when the page is first visited, id the input has a value, execute the script?

Upvotes: 0

Views: 1224

Answers (3)

SouthShoreAK
SouthShoreAK

Reputation: 4296

Well, you can use the onload event. You may also want to consider using Jquery's document.ready functionality. The difference can be found here. Also, Jquery provides a much more concise syntax for accessing your form elements

In either case, you would probably simply need function that does something like checking the field has a value.

<script type="text/javascript">  

function showUserOnLoad()
{
    var inputValue = $('#sku1').val();
    if(inputValue != '' && inputValue != null)
        showUser(1, inputValue);        
}

function showUser(userNumber, str)  
{
    ...
} 
</script>

Upvotes: 0

COLD TOLD
COLD TOLD

Reputation: 13569

you can also use the ready function from the jquery

$(document).ready(function() {
  showUser(1, $("#sku1").val());
});

Upvotes: 0

Bergi
Bergi

Reputation: 664195

Just execute the function onload?

window.onload = function init() {
    showUser(1, document.getElementById("sku1").value);
}

You may also use the onload attribute of your document's body, or add the init function as an event handler to DOMContentLoaded. But I'm sure you already have some functions which are executed onDOMready.

Upvotes: 2

Related Questions