SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need some help from javascript gurus, javascript events and conditions

    Hi guys

    I am having a select command like this -

    Code:
    <select name="nlone" id="nlone" onchange="ajaxFunction()">
    What I want, is that the function ajaxFunction() should be called onLoad first. Then, once user selects a value the onChange event should be called, calling the same function, of course.

    Any idea? Thanks.

    Edit:

    I don't have the body tag in my disposition here, so I can't use the onload in the body tag, I have to use it somewhere inside the page.

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sounds like what you want is the following code which should be inserted in the head off your document

    Code:
    <script type="text/javascript">
    window.onload = function(){
      ajaxFunction();
    }
    </script>

  3. #3
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, but I can't use the body tag or anything above ... since for those i'm using a common php include. I've to call it somewhere inside the body.

    Edit:

    Well, I put it inside an image tag, but seems there's some problem somewhere ... it's not working as I want ...


    Basically, this is a part of my ajax code that I want to execute

    Code:
    	box = document.forms[0].nlone;
    	var age = document.getElementById('nlone').value;
    	<? if ($thisq!='') { ?>
    	if (age == '') {
    	age = <?=$thisq?>;
    	} 
    	<? } ?>
    	var queryString = "?q=" + age + "&sid="+Math.random();
    Means, I am trying to play with three conditions

    1. If there is no $thisq (php variable) and no age (javascript variable), then load tha page normally
    2. If there is $thisq but no age, make age = $thisq and execute the page
    3. If there is age, irrespective of whether $thisq is set or not, execute the page

    Now, when I am having the value of age, (ie, onchange in select), the function works as I want. However, when the page loads with a $thisq value, nothing happens.

    Below is the full ajax code -

    Code:
    <script language="javascript" type="text/javascript">
    <!-- 
    //Browser Support Code
    function ajaxFunction(){
    	
    	var ajaxRequest;  // The variable that makes Ajax possible!
    	
    	try{
    		// Opera 8.0+, Firefox, Safari
    		ajaxRequest = new XMLHttpRequest();
    	} catch (e){
    		// Internet Explorer Browsers
    		try{
    			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    			try{
    				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch (e){
    				// Something went wrong
    				alert("Your browser broke!");
    				return false;
    			}
    		}
    	}
    	// Create a function that will receive data sent from the server
    	ajaxRequest.onreadystatechange = function(){
    		if(ajaxRequest.readyState == 4){
    			var ajaxDisplay = document.getElementById('ajaxDiv');
    			ajaxDisplay.innerHTML = ajaxRequest.responseText;
    		}
    	}
    	box = document.forms[0].nlone;
    	var age = document.getElementById('nlone').value;
    	<? if ($thisq!='') { ?>
    	if (age == '') {
    	age = <?=$thisq?>;
    	} 
    	<? } ?>
    	var queryString = "?q=" + age + "&sid="+Math.random();
    	ajaxRequest.open("GET", "getlivra.php" + queryString, true);
    	ajaxRequest.send(null); 
    }
    
    //-->
    </script>

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can still use the code in my previous message just place it where ever you can in the body, the code will still execute when the page loads.

  5. #5
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,563
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Mortimer. I've put that inside an image tag, but it seems there's a problem somewhere ...

    Edit:

    It's working now ... I had to refresh the page ...

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool glad its all sorted


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •