...

View Full Version : anyone interested in helping me make a function



gerble1000
12-24-2011, 02:33 PM
i need to make a function that will send a string to my perl script then recieve the answer and set a div innerhtml to the answer..
i plan on sending the string from the div onload as i will have about 30 divs that need changing.




<body>
<div name="example1" onload="javascript:retrieve('getstring1')">my retieved string1</div>

<div name="example2" onload="javascript:retrieve('getstring2')">my retieved string2</div>

</body>


<head>
<SCRIPT language="JavaScript">
function retieve('var') {

}
</SCRIPT>



</head>





im not quite sure where to go with this but i can easily make the perl script do if statements on the string and return the correct string to put into the div.
the reason for this is that the string will be changed every couple weeks.
there is probably a better way to do this but this would be more appropriate for me :)

Philip M
12-24-2011, 04:02 PM
JavaScript is purely a client-side language, and unless used with an Ajax call has no capability to read from or write to a file (except a cookie),
communicate with the server, access a database, the client's operating system or the Windows registry, or alter the default behaviour of the browser.

You should use Ajax.


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

gerble1000
12-24-2011, 04:38 PM
JavaScript is purely a client-side language, and unless used with an Ajax call has no capability to read from or write to a file (except a cookie),
communicate with the server, access a database, the client's operating system or the Windows registry, or alter the default behaviour of the browser.

You should use Ajax.


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

ok ajax sounds fine
would you have a way of doing this with ajax.

gerble1000
12-24-2011, 04:55 PM
ok i found this code

<script type="text/javascript">
var http = createRequestObject();
function createRequestObject() {
var objAjax;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
objAjax = new ActiveXObject("Microsoft.XMLHTTP");
}else{
objAjax = new XMLHttpRequest();
}
return objAjax;
}

function getNewContent(){
http.open('get','newcontent.txt');
http.onreadystatechange = updateNewContent;
http.send(null);
return false;
}

function updateNewContent(){
if(http.readyState == 4){
document.getElementById('mySentence').innerHTML = http.responseText;
}
}
</script>

how would i modify this to do what i want.
i notice that it retieves data from a txt file wich would be ideal.
i will make multiple txt files named the same as the div i would like to change.
no i see i could just make a script for each div but that would be foolish
how can i make it so i can use this 1 script for all my divs
so each div would call this like <div id="nameofdiv1"onload="javascript:getnewcontent('nameofdiv1')".>

gerble1000
12-24-2011, 07:19 PM
ok i have made a script that works
but there is an issue, when run it only shows the last one in the load script.
the rest say loading, i know i have messed this up somewhere but if you could look over the code while i keep playing that would be great.
its got to be a return; missing or something


<script type="text/javascript">
var http = createRequestObject();
function createRequestObject()
{
var objAjax;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
objAjax = new ActiveXObject("Microsoft.XMLHTTP");
}else{
objAjax = new XMLHttpRequest();
}
return objAjax;

}
</script>

<script type="text/javascript">
function getNewContent(folder,divid)
{



http.open('get',folder);
http.onreadystatechange = updateNewContent;
http.send(null);
return false;



function updateNewContent()
{

if(http.readyState == 4)
{
document.getElementById(divid).innerHTML = http.responseText;

}


}

}

</script>








<script type="text/javascript">
function load1()
{
getNewContent('1/available.txt','sold1');
getNewContent('2/available.txt','sold2');
getNewContent('3/available.txt','sold3');
getNewContent('4/available.txt','sold4');
getNewContent('5/available.txt','sold5');

}



</script>

gerble1000
12-24-2011, 07:20 PM
ok i have made a script that works
but there is an issue, when run it only shows the last one in the load script.
the rest say loading, i know i have messed this up somewhere but if you could look over the code while i keep playing that would be great.
its got to be a return; missing or something


<script type="text/javascript">
var http = createRequestObject();
function createRequestObject()
{
var objAjax;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
objAjax = new ActiveXObject("Microsoft.XMLHTTP");
}else{
objAjax = new XMLHttpRequest();
}
return objAjax;

}
</script>

<script type="text/javascript">
function getNewContent(folder,divid)
{



http.open('get',folder);
http.onreadystatechange = updateNewContent;
http.send(null);
return false;



function updateNewContent()
{

if(http.readyState == 4)
{
document.getElementById(divid).innerHTML = http.responseText;

}


}

}

</script>








<script type="text/javascript">
function load1()
{
getNewContent('1/available.txt','sold1');
getNewContent('2/available.txt','sold2');
getNewContent('3/available.txt','sold3');
getNewContent('4/available.txt','sold4');
getNewContent('5/available.txt','sold5');

}



</script>



this is triggered bt the <body onload="load1()">

gerble1000
12-24-2011, 08:11 PM
ok i have played and played with no joy.
it looks like on the onload event it goes through all 5 functions
then once its done all 5 it only displays the last one.
if i had only 1 function in the load1 it will work
as soon as i put a second function the first one becomes blank and second works.
with 5 the 5th works and the rest are blank.
what have i done

hdewantara
12-25-2011, 06:08 PM
You have created 1 ajax object/ channel called "http". So when the <body> calls 5 successive ajax calls, each would override previous requests.

I think this would have been successful if 5 ajax objects were created, but managing them is a bit harder. The easier way probably is by using "synchronous" ajax. So if you call 5 ajax calls, each would wait for the previous to complete. Also, only 1 ajax object is required.

Let's see if this works (see the changes in red):

...
<script type="text/javascript">
var http = createRequestObject();

function createRequestObject() {
var objAjax;
var browser = navigator.appName;
if (browser == "Microsoft Internet Explorer"){
objAjax = new ActiveXObject("Microsoft.XMLHTTP");
}else{
objAjax = new XMLHttpRequest();
}
return objAjax;
}

function getNewContent(folder,divid){
//function below is for async.ajax, so isn't needed.
//http.onreadystatechange = updateNewContent;

http.open('get',folder,false); //don't forget to include 3rd param for sync.ajax
http.send(null);
document.getElementById(divid).innerHTML = http.responseText;
// return false;this isn't needed.
}

function load1(){
getNewContent('1/available.txt','sold1');
getNewContent('2/available.txt','sold2');
getNewContent('3/available.txt','sold3');
getNewContent('4/available.txt','sold4');
getNewContent('5/available.txt','sold5');
}
</script>
</head>

<body onload="load1()">
...

gerble1000
12-25-2011, 09:01 PM
dude that worked perfectly.
thanx



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum