SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    load data into js

    could anyone suggest a technique (or suggest a number of techniques) to load data into a global scope?

    The following works in FF but not IE (... and it uses eval which I don't like)

    Code:
    function temp(){
    	var temp = loadURL('data.js');
    	window.eval(temp);
    	return true;
    }
    
    function loadURL(url) {
    	var content = '';
    	var value = false;
    	
    	if (window.XMLHttpRequest) {
    		request = new XMLHttpRequest();
    	} else if (window.ActiveXObject) {
    		request = new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	
    	if (request) {
    		request.open("GET", url, false);
    		request.send(null);
    		if (request.status == 200){
    			return request.responseText;
    		}else{
    			return 'Error: ' + request.status + ' ' + request.statusText;
    		}
    	}
    }
    where data.js is a simple array declaration
    var aData = new Array ([1,0,0,3,0,'test']);

    It's early on in coding this so I'm still flexible. My app needs data located in a database so any other methods of getting this data (via ajaxish) are also appreciated.

    Curiously enough the above as mentioned only works in FF and not IE but this works in both
    Code:
    function temp(){
    	var temp = "var aData = new Array ([1,0,0,3,0,'test']);";
    	window.eval(temp);
    	return true;
    }
    ... strange

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check out JSON

    http://json.org/

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm using prototype someone directed me to evalScripts. my code which still does not work in IE now looks like this:

    Code:
    var aVenues = "";
    var myAjax = new Ajax.Request(
    	'/js/data.js',
    	{method: 'get', onComplete: ajax_response, asynchronous:true }
    );
    
    function ajax_response(x){
    	alert(x.responseText); /*debug 1 */
    	x.responseText.evalScripts();
    	alert(aVenues); /* debug 2 */
    }
    WHERE:

    data.js:
    <script type="text/javascript">
    var aData = new Array ([1,0,0,3,0,'test']);
    </script>

    debug alerts in FF is:

    <script type="text/javascript">
    var aData = new Array ([1,0,0,3,0,'test']);
    </script>


    followed by
    1,0,0,3,0,'test


    debug alerts in IE is:

    <script type="text/javascript">
    var aData = new Array ([1,0,0,3,0,'test']);
    </script>


    followed by a null alert box


    Hope that's enough details/the problem makes sense help appreciated ask for clarification where needed

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Should:

    var aData = new Array ([1,0,0,3,0,'test']);

    be either:

    var aData = new Array (1,0,0,3,0,'test');

    or:

    var aData = [1,0,0,3,0,'test'];

    or are you trying to create an array that has one entry that is an array?
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    data.js was an abridged version it had multiple entries one which was an e with an accent that was causing ie to break at the eval statement


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
  •