Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Nov 2011
    Posts
    131
    Thanks
    11
    Thanked 0 Times in 0 Posts

    anyone interested in helping me make a function

    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.

    Code:
    <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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,298
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Regular Coder
    Join Date
    Nov 2011
    Posts
    131
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Thumbs up

    Quote Originally Posted by Philip M View Post
    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.

  • #4
    Regular Coder
    Join Date
    Nov 2011
    Posts
    131
    Thanks
    11
    Thanked 0 Times in 0 Posts
    ok i found this code
    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')".>

  • #5
    Regular Coder
    Join Date
    Nov 2011
    Posts
    131
    Thanks
    11
    Thanked 0 Times in 0 Posts
    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

    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;
    
    }
    </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>

  • #6
    Regular Coder
    Join Date
    Nov 2011
    Posts
    131
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by gerble1000 View Post
    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

    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;
    
    }
    </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()">

  • #7
    Regular Coder
    Join Date
    Nov 2011
    Posts
    131
    Thanks
    11
    Thanked 0 Times in 0 Posts
    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

  • #8
    Regular Coder hdewantara's Avatar
    Join Date
    Aug 2009
    Location
    Jakarta, Indonesia.
    Posts
    375
    Thanks
    9
    Thanked 55 Times in 54 Posts
    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):
    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(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()">
    ...

  • #9
    Regular Coder
    Join Date
    Nov 2011
    Posts
    131
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Thumbs up

    dude that worked perfectly.
    thanx


  •  

    Posting Permissions

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