473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Load data from external files

Hi,
I have a file containing variables defined in javascript syntax, like,
var a = 15;
var list = [ 'a','b','c'];
..
..
I want to load this external file dynamically and read in the data.
I can successfuly read this file content if I include the file
statically in the head section in my page. But when I am trying to
include the file using DHTML, i cannot source it.
Please suggest.

Regards,
Suvajit

Mar 8 '06 #1
6 7005
jeet_sen said the following on 3/7/2006 10:58 PM:
Hi,
I have a file containing variables defined in javascript syntax, like,
var a = 15;
var list = [ 'a','b','c'];
..
..
I want to load this external file dynamically and read in the data.
I can successfuly read this file content if I include the file
statically in the head section in my page. But when I am trying to
include the file using DHTML, i cannot source it.
Please suggest.


Show some source code on how you are loading it and attempting to access it.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 8 '06 #2
I am loading the data with the following function:
function loadScript (url, id, callback) {
var scriptElement = document.createElement('script');
scriptElement.type = 'text/javascript';
scriptElement.src = url;
scriptElement.id = id;
if (typeof scriptElement.addEventListener != 'undefined') {
scriptElement.addEventListener(
'load',
function (evt) { callback(); },
false
);
}
else if (typeof scriptElement.attachEvent != 'undefined') {
scriptElement.attachEvent(
'onreadystatechange',
function () {
if (scriptElement.readyState == 'complete') {
callback();
}
}
);
}
document.getElementsByTagName('head')[0].appendChild(scriptElement);

}
Function call:
// Load data
file2Load = 'test.js';
loadScript(file2Load,name,function () { alert(file2Load+"
loaded!!");} );

test.js looks like this:

var TABLE_ITEMS = {
'name' : 'GS60H',
'level' : 'Library',
'child' : {
'gs60hcustom' : {
'qualis'
: {'status' : 'PASSED' },
'startAttributeCheck'
: {'status' : 'WAIVED' },
'availabilityCheck'
: {'status' : 'PASSED' },
'libAttributesCheck'
: {'status' : 'PASSED' },

'checkViewConsistency' : {'status' : 'PASSED' },
'simModelsCompile'
: {'status' : 'FAILED' },
},
},
'total' : 500,
'failed' : 20,
'waive' : 5
};
* Used callback to ensure that my script loading function is working
properly.
Found that the passed function has been called and new script tag
has been created successfully.
* But I cannot access the object TABLE_ITEMS defined in test.js. What
is the scope of the object loaded through a script dynamically?

Mar 8 '06 #3
jeet_sen wrote:
I am loading the data with the following function:
The function is fine, your data file has syntax errors.

function loadScript (url, id, callback) {
var scriptElement = document.createElement('script');
scriptElement.type = 'text/javascript';
scriptElement.src = url;
scriptElement.id = id;
if (typeof scriptElement.addEventListener != 'undefined') {
scriptElement.addEventListener(
'load',
function (evt) { callback(); },
false
);
}
else if (typeof scriptElement.attachEvent != 'undefined') {
scriptElement.attachEvent(
'onreadystatechange',
function () {
if (scriptElement.readyState == 'complete') {
callback();
}
}
);
}
document.getElementsByTagName('head')[0].appendChild(scriptElement);

}
Function call:
// Load data
file2Load = 'test.js';
loadScript(file2Load,name,function () { alert(file2Load+"
loaded!!");} );
There is a syntax error here from auto-wrapping. When posting code,
manually wrap at about 70 characters so it can be quoted a couple of
times without wrapping.

var file2Load = 'test.js';
loadScript(file2Load,name,function () {
alert(file2Load + " loaded!!");

// Test availability of TABLE_ITEMS
alert(TABLE_ITEMS.name);
} );


But that's not the really problem...

test.js looks like this:

var TABLE_ITEMS = {
'name' : 'GS60H',
'level' : 'Library',
'child' : {
'gs60hcustom' : {
'qualis'
: {'status' : 'PASSED' },
'startAttributeCheck'
: {'status' : 'WAIVED' },
'availabilityCheck'
: {'status' : 'PASSED' },
'libAttributesCheck'
: {'status' : 'PASSED' }, --------------------------^^^

the extra comma here causes problems.


'checkViewConsistency' : {'status' : 'PASSED' },
'simModelsCompile'
: {'status' : 'FAILED' },
}, ---------------------------------------------------^^^

As does this one. Formatting for posting would have helped. Firefox
was happy with it (surprisingly), only IE barfed for me.
},
'total' : 500,
'failed' : 20,
'waive' : 5
};


It's probably better to use machine-generation of such code, it's very
fussy doing it manually. Try building a form that generates the code in
a text area for copy/paste (for your local use only of course).

Try this version:
var TABLE_ITEMS = {
'name' : 'GS60H',
'level' : 'Library',
'child' : {
'gs60hcustom' : {
'qualis' : {'status' : 'PASSED' },
'startAttributeCheck' : {'status' : 'WAIVED' },
'availabilityCheck' : {'status' : 'PASSED' },
'libAttributesCheck' : {'status' : 'PASSED' },
'checkViewConsistency': {'status' : 'PASSED' },
'simModelsCompile' : {'status' : 'FAILED' }
}
},
'total' : 500,
'failed' : 20,
'waive' : 5
};

--
Rob
Mar 8 '06 #4
Hi Rob,
Thanks a lot for your effort and time. I will surely try it out.
My data files will be machine generated. I generated the file manually
just to implement my plan.
Lately I have worked on an alternative plan and have implemented it.
Instead of the object literal form I dumped my data in XML form and it
is working fine.
Please suggest what will be a better option.
Regards,
Suvajit

Mar 8 '06 #5
jeet_sen wrote:
Hi Rob,
Thanks a lot for your effort and time. I will surely try it out.
My data files will be machine generated. I generated the file manually
just to implement my plan.
Lately I have worked on an alternative plan and have implemented it.
Instead of the object literal form I dumped my data in XML form and it
is working fine.
Please suggest what will be a better option.


I can't tell you which is better - have a look at JSON:

<URL:http://www.json.org/>
--
Rob
Mar 8 '06 #6
RobG wrote:
jeet_sen wrote:
var TABLE_ITEMS = {
'name' : 'GS60H',
'level' : 'Library',
'child' : {
'gs60hcustom' : {
'qualis'
: {'status' : 'PASSED' },
'startAttributeCheck'
: {'status' : 'WAIVED' },
'availabilityCheck'
: {'status' : 'PASSED' },
'libAttributesCheck'
: {'status' : 'PASSED' },

--------------------------^^^

the extra comma here causes problems.


'checkViewConsistency' : {'status' : 'PASSED' },
'simModelsCompile'
: {'status' : 'FAILED' },
},

---------------------------------------------------^^^

As does this one. Formatting for posting would have helped. Firefox
was happy with it (surprisingly), only IE barfed for me.


It is not surprising to me, because JavaScript is known
to work as specified in ECMAScript here; JScript does not.
PointedEars
Mar 8 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: jason baumunk | last post by:
I author applications in php which use external stylesheets. When viewed through ie 6, netscape 6, et al. occassionally the stylesheets will not load or be applied. Has anybody encountered this...
1
by: Ray in HK | last post by:
What are the differences between LOAD DATA INFILE and LOAD DATA LOCAL INFILE ? I found some web hosting company do not allow using LOAD DATA INFILE but allow LOAD DATA LOCAL INFILE. The reason...
2
by: jay | last post by:
hi, Question on Load/import command. consider a sample table create table table_name ( col1 timestamp not null default current timestamp, col2 int, col3 int, col4 int, primary key(col1) );...
10
by: GeekBoy | last post by:
Okay, I have two identical web servers running Windows 2003 web server. I have an ASP.NET application which runs great on one of them. Dedicated IP address, behind our firewall, etc. Everyone's...
3
by: eieiohh | last post by:
MySQL 3.23.49 PHP 4.3.8 Apache 2.0.51 Hi All! Newbie.. I had a CRM Open Source application installed and running. Windows Xp crashed. I was able to copy the contents of the entire hard...
2
by: Magnus | last post by:
I'm currently developing an application with classified information as input to a couple of algorithms. Which strategy should I use to protect the input data from beeing read? The files should...
2
by: Jeff Allan | last post by:
Hello, I am trying to load an external HTML page into a DIV tag with .NET 05 and I can't figure it out. Any suggestions? My Scenario: - Gridview populated with list of available files - I...
8
by: John Dunn | last post by:
Since currently we aren't allowed to have compiled XAML files embedded in C++ apps I'm using Markup::XamlReader::Load to dynamically load XAML files. This works perfectly fine with external files...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.