View Full Version : Connecting to Database using Javascript both in IE and FireFox

12-12-2006, 04:48 AM
HI Everyone,
I am trying to connect to the client database from the application deployed in server. I have done this using ActiveXObjects. It is working fine in IE but problem with FireFox. Since FF don't allow ActiveXObjects i decided to do this without using ActiveXObjects.
It is very urgent can anyone help me in this.

Thanks in advance...

12-12-2006, 05:31 AM
ActiveX is weird. It's IE only, and has special stuff it can do (and presents security risks, etc. to the user).

Aside from ActiveX, which, as I said, is a weird exception, javascript cannot connect to a database. Rather, you must use PHP or another server side language to do so.

The only other similar option is Java (not javascript, but a Java applet) since it can do many things similar to ActiveX and is friendly to many browsers, though the user, for each, will need the Java plugin, and, since is presents security holes and risks, the user must allow it to have access.

12-12-2006, 05:51 AM

Thanks for giving advice.
Could you pls help me by providing the sample code to implement the solution.

12-12-2006, 06:02 AM
Activex works without much problems on the server-side.

In ASP, ASP.NET they employ Activex.

Why don't you post the code you were using or the language preference for this job.

12-12-2006, 06:29 AM

Code below is what i am using. This is working fine with IE, but problem with FF. Since FF doesn't support ActiveXObjects.

I need a sample code to connect client database from the server application(testDB.asp) which is compatible with IE and FF

pls help me it is very urgent.

filename: testDB.asp
<script type="text/javascript">
var conn = new ActiveXObject("ADODB.Connection")
var conn_str = ""
var dbHost = ""
var dbUser = ""
var dbPassword = ""
var dbProvider = ""
var dbDefault = ""
function getData()
dbHost = "localhost";
dbUser = "sa";
dbPassword = "";
dbProvider = "SQLOLEDB";
dbDefault = "Northwind";
conn_str = "Provider="+dbProvider+";Data Source="+dbHost+"; User Id="+dbUser+"; password="+dbPassword+"; Initial Catalog="+dbDefault;
function getDatabase()
alert("Error creating Connection")
<body onload="getData()">


12-12-2006, 06:55 AM
Instead of using your database manipulation code on the client side do it in the server side so that your web server will process whatever database manipulation script before sending the results to the client browser.

It is not appropriate to have a database manipulating script in the client side script due to security reasons also.

You can checkout this website (http://research.nihonsoft.org/javascript/ssjshtml/nes/ssjs/ServerJSv14/Guide-sanborns/Server/Online/Output/contents.htm) which describe using JavaScript as a server-side scripting language as well as client side.

12-12-2006, 07:15 AM

Thanks for the help.
I have tried this earlier, but the problem is with DB connections across domains.
As per my knowledge since the application is placed on the webserver it is not possible to connect client DB from server.
Also i have the requirement to provide an interface to the users such that they can connect to their own databases(eg: SQLSERVER) simply by providing the DB details of localhost.

My approches so far i have done:
1) Initially i have tried this all with asp, but it raised an issue of DB connection not possible across domains.
2) Second i have tried this by completely migrating the application to client side using JS. but raised the problem with ActiveXobjects

Hope u got my problem. pls help me very urgent

Thanks in advance...

12-12-2006, 08:39 AM
I am having difficulty with the way you are specifying your database location. Usually in a website development database will be either stored in the machine where your site resides or on another machine with which the web server machine has connectivity.

In your case you are telling that you need to connect to the databases that are in client machine(s). Are you telling that each user has their own database in their choosen database package?

Is this something not related to the normal web development process?

12-12-2006, 09:13 AM
yes, you are right. I need to manipulate the databases of client's from the server.
Could you pls help me in what way i can proceed with this kind of requirement using ASP and JS with SQLSERVER.

Thanks in advance. very urgent...

12-12-2006, 10:33 AM
I think the JavaScript code that does communicate with the database in a client machine only works in Microsoft Internet Explorer as it uses Activex technology which is not compatible with Mozilla based browsers as the code runs at the client browser and the script engine of the browser is responsible for the execution.

If you are looking for a way using which you are trying to manipulate the database of your clients/visitors, i don't think it as a reliable method for doing so as a client can disable the JavaScript of the browser at their will.

Because of this reason all the important operations like database manipulation are done at the server-side.

You can keep a centralised database rather than having a database on each of your clients, which is not practical at all due to so many reasons like accessibility, security, etc. If the client makes any change in the database (which can be done at their will as the database is stored in their machine) that will create problems for your script as a result you won't get the result as supposed.

I don't know what type of application/task you are dealing with but it would be better if you redesign the current application design.

You can post your problem in the forum so that you'll get more good ideas from the forums users and carry out your job in an efficient manner.

12-14-2009, 04:45 PM
I'd like to revive this post from death because i've got a similar issue.

I'm trying to write a browser based program that will manipulate a local database. It won't be on a server so I can't use any server side languages. I know I can use javascript to connect to an access database but that only seems to work in IE and the purpose of this program being browser based is so it isn't os or browser specific.
Any ideas?


12-15-2009, 07:14 AM
You are then using a browser for a task it is not really designed for: browsers are meant for browsing the web, not acting as applications. To be blunt, that's like trying to hit a nail with a toothpick. Entirely different tool for an entirely different purpose, and sure there may be some especially powerful toothpicks, but you're not going to find a solution that will work for every toothpick to hit every nail.

Certainly they can be useful in other ways, but their general support for things that deviate too much from the norms will drop off and often end up browser specific.

You can in fact use server side code. In this case, the browser and the server are the same machine. That would mean setting up the computer as a web server, though.

Javascript is not designed to do this. Here's why:
1. Javascript has no access to the server (because the user would then have access to easy hacking methods).
2. Javascript has no access to the local machine (because the website coder would then have access to the local machine through Javascript, like stealing files).
Because of this, Javascript is ONLY useful for working within a browser for specifically browser-related tasks, like manipulating layouts in HTML or working with data in forms. There are very few exceptions to this, and there are no cross-browser general exceptions that would allow you to manipulate a database.

I don't know of a decent way to make this really work cross-browser. With other additions to the browser there are ways, for example:
1) Firefox extensions. I don't know exactly how these work, but they have a lot less security working against them and you can work with files on the local machine easily. An ftp extension I use allows manipulation of (storing/deleting) local files, for example.
2) ActiveX. ActiveX is a pain, though, because it is IE-only and it is a security risk so often disabled. However, if you are making this as a tool for yourself and possibly other "trained" users, then that's no problem. Just never use firefox (or other browsers), and tell them to do the same. That's the easy solution to your problem here.
3) Use an applet of some sort, like a Java applet. But once you do that why not just make an exe with Java?

In the end, there are two options: 1) use a working but limited solution (like activex only in IE), or 2) move on to a desktop programming language that is actually meant to handle this sort of thing, such as Java or C++.

If you really want this to be browser-based, then I suggest looking into some of the more creative options allowed within certain browsers, such as firefox extensions. That's where I'd start if I wanted to make this a powerful and stable application. Of course ActiveX, though, may be a bit easier if you don't mind always using IE (and ActiveX).