SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
-
Apr 12, 2007, 01:21 #1
- Join Date
- May 2004
- Location
- Paris
- Posts
- 1,565
- 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()">
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.
-
Apr 12, 2007, 01:27 #2
- 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>
-
Apr 12, 2007, 01:29 #3
- Join Date
- May 2004
- Location
- Paris
- Posts
- 1,565
- 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();
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>
-
Apr 12, 2007, 01:36 #4
- 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.
-
Apr 12, 2007, 01:44 #5
- Join Date
- May 2004
- Location
- Paris
- Posts
- 1,565
- 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 ...
-
Apr 12, 2007, 01:54 #6
- Join Date
- Nov 2006
- Posts
- 68
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Cool glad its all sorted
Bookmarks