I have a script to create an ActiveX component that works fine when run from the command line but reports:
SCRIPT429: Automation server can't create object
when run from JavaScript in a html page. I know the web page JavaScript works OK when I try and create a different ActiveX component like Excel.Application so I think it is something about the particular ActiveX component I am trying to create.
How can I debug this? Id there some flag I can check to see if the ActiveX component will not allow itself to be created in a web page?
The web page JavaScript looks like this:
<script language="javascript" >
function MakeOne()
{
var obj = new ActiveXObject('ECRUtilATL.Transaction');
obj.Amount1In = "12.53";
var result = "";
if (obj == null) {
result = 'null';
}
else {
result = 'not null';
}
alert(result);
}
</script>
Related
I am using google app scripts on google sites. I have created a navigation menu, and I embedded it into the page. I want to get the pageURL() from google scripts and retrieve it in my JavaScript page. I tried using the scriptlet to get the value, but it doesn't execute. Here is what I have so far. How can I get access to values in google app scripts and use them in my JavaScript function?
google script (.gs)
function getPageName(){
var site = SitesApp.getSite("site.com", "sitename");
var page = site.getChildren()[0];
var pageName = page.getUrl().split("/").splice(-1)[0];
return pageName;
}
javascript file
var pageName = <?!= getPageName()?>; // doesnt execute, need to get page url
if(pageName == linkName){
// add class here.
}
Since google loads the apps script as an iframe, I tried doing window.location.href, but it doesn't work either. The page name ends up being the name of the google app instead.
An alternative to using scriptlets is to use google.script.run (Client-side API)
It's pretty easy to use. In your case, it should be like this
code.gs
function getPageName(){
var site = SitesApp.getSite("site.com", "sitename");
var page = site.getChildren()[0];
var pageName = page.getUrl().split("/").splice(-1)[0];
return pageName;
}
Javascript File:
function onSuccess(receviedPageName)
{
if(receviedPageName== linkName)
{
// add class here.
}
}//onSuccess
google.script.run.withSuccessHandler(onSuccess).getPageName();
withSuccessHandler(function) is executed if the server-side function returns successfully or withFailureHandler(function) is executed if a server side function fails to complete the task it was assigned.
Give it a try :)
In my web-based Flex app I make an external interface call to this method:
var arr:Array = ExternalInterface.call("getClientData", "");
Here is the method in my HTML page:
function getClientData( keys ) {
try {
mySearchIntegration = new ActiveXObject( "pkg.myView.ExternalIntegration.getData" );
var myObj = "";
var cust = "";
var custID = "";
var custEMAIL = "";
var custNAME = "";
myObj = mySearchIntegration.GetData("44277F-XUI18");
}
catch (e) {
}
}
The function returns data.
Now, if I I try invoking the same function directly from HTML app (my JSP page on a non-IE browser), I get the following error: "ActiveXObject not defined".
Now, that makes sense to me because ActiveXObject is only supported by IE.
So, why does it work when running out of a Flex app (in a non-IE web browser) but I get the error running it as a web app from my JSP page?
My assumption wasn't correct, it doesn't work in a non-IE browser even if I have a Flex object embedded in the page.
The problem was the way I was stubbing my method out; the Flex app/External Interface connection didn't throw an error (even if there was one), and the HTML/JSP approach always did.
I'm working on some code that needs to parse numerous files that contain fragments of HTML. It seems that jQuery would be very useful for this, but when I try to load jQuery into something like WScript or CScript, it throws an error because of jQuery's many references to the window object.
What practical way is there to use jQuery in code that runs without a browser?
Update: In response to the comments, I have successfully written JavaScript code to read the contents of files using new ActiveXObject('Scripting.FileSystemObject');. I know that ActiveX is evil, but this is just an internal project to get some data out of some files that contain HTML fragments and into a proper database.
Another Update: My code so far looks about like this:
var fileIo, here;
fileIo = new ActiveXObject('Scripting.FileSystemObject');
here = unescape(fileIo.GetParentFolderName(WScript.ScriptFullName) + "\\");
(function() {
var files, thisFile, thisFileName, thisFileText;
for (files = new Enumerator(fileIo.GetFolder(here).files); !files.atEnd(); files.moveNext()) {
thisFileName = files.item().Name;
thisFile = fileIo.OpenTextFile(here + thisFileName);
thisFileText = thisFile.ReadAll();
// I want to do something like this:
s = $(thisFileText).find('input#txtFoo').val();
}
})();
Update: I posted this question on the jQuery forums as well: http://forum.jquery.com/topic/how-to-use-jquery-without-a-browser#14737000003719577
Following along with your code, you could create an instance of IE using Windows Script Host, load your html file in to the instance, append jQuery dynamically to the loaded page, then script from that.
This works in IE8 with XP, but I'm aware of some security issues in Windows 7/IE9. IF you run into problems you could try lowering your security settings.
var fileIo, here, ie;
fileIo = new ActiveXObject('Scripting.FileSystemObject');
here = unescape(fileIo.GetParentFolderName(WScript.ScriptFullName) + "\\");
ie = new ActiveXObject("InternetExplorer.Application");
ie.visible = true
function loadDoc(src) {
var head, script;
ie.Navigate(src);
while(ie.busy){
WScript.sleep(100);
}
head = ie.document.getElementsByTagName("head")[0];
script = ie.document.createElement('script');
script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js";
head.appendChild(script);
return ie.document.parentWindow;
}
(function() {
var files, thisFile, win;
for (files = new Enumerator(fileIo.GetFolder(here).files); !files.atEnd(); files.moveNext()) {
thisFile = files.item();
if(fileIo.GetExtensionName(thisFile)=="htm") {
win = loadDoc(thisFile);
// your jQuery reference = win.$
WScript.echo(thisFile + ": " + win.$('input#txtFoo').val());
}
}
})();
This is pretty easy to do in Node.js with the cheerio package. You can read in arbitrary HTML from whatever source you want, parse it with cheerio and then access the parsed elements using jQuery style selectors.
I make program to read xml file by using traditional JavaScript.
<script type="text/javascript">
var xmlDoc;
function loadxml(sImportXML) {
if( window.ActiveXObject && /Win/.test(navigator.userAgent) ) {
xmlDoc = new ActiveXObject("Msxml.DOMDocument");
xmlDoc.async = false;
xmlDoc.onreadystatechange = function () {
if (xmlDoc.readyState == 4) readXML();
}
xmlDoc.load(sImportXML);
}
else if( document.implementation && document.implementation.createDocument ) {
xmlDoc = document.implementation.createDocument("","",null);
xmlDoc.async=false;
alert(sImportXML);
var loaded = xmlDoc.load(sImportXML);
if (loaded) {
readXML();
}
}
else {
alert("Your browser can\'t handle this script");
return;
}
}
<body onload="loadxml('../XML/Question.xml');">
Upper loadxml function run correctly at IE but not at firefox.
alert line display this value ../XML/Question.xml.
but xmlDoc.Load function did not run correctly.
It reply error Access to restricted URI denied
Please anyone help me.
In Firefox the javascript will not let you access files on the user's local file system; there's simply no way to do it. That would be a huge security breach.
This works in Internet Explorer with ActiveX because ActiveX is a plug-and-play application module system (kind of like mini browser plugins) where the apps, like 'Msxml.DOMDocument', have more power than just javascript and can access files on the user's local file system.
But document.implementation is regular javascript, so it has all normal security restrictions, a major one being that the user's file system is off limits.
If the XML file is on the server, you can embed in your html code like this:
<script id="the-xml" type="text/xml">
....your xml document contents here....
</script>
Then you can get the contents in javascript like this:
var sImportXML = document.getElementById('the-xml').text;
But depending on your application, it might make sense to not use the javascript xml at all. Usually, you'd parse the xml on the server side, and communicate with javascript in json or html snippets.
On IIS6, I can use WMI to list available websites, like this:
var iis = GetObject("winmgmts://localhost/root/MicrosoftIISv2");
var query = "SELECT * FROM IIsWebServerSetting"
// get the list of virtual servers
var results = iis.ExecQuery(query);
for(var e = new Enumerator(results); !e.atEnd(); e.moveNext()) {
var site = e.item();
// site.Name // W3SVC/1, W3SVC/12378398, etc
// site.Name.substr(6) // 1, 12378398, etc
// site.ServerComment) // "Default Web Site", "Site2", etc
// site.ServerBindings(0).Port // 80, 8080, etc
}
I know I can run this script on IIS7, if I have previously installed the IIS6 Compatibility Pack.
Is it possible to get the list of WebSites without requiring the compatibility pack as a pre-requisite?
I know I can run AppCmd to do this from the command line:
\Windows\system32\inetsrv\appcmd list sites
But... can I run that from a custom action in an MSI?
And... if not, how can I do the equivalent thing (list websites on IIS7) from javascript?
EDIT
Here's how I tried running the command from within Javascript.
function GetWebSites_IIS7()
{
var ParseOneLine = function(oneLine) {
...a bunch of regex parsing here....
};
LogMessage("GetWebSites_IIS7() ENTER");
var shell = new ActiveXObject("WScript.Shell");
var windir = shell.Environment("system")("windir");
// aka Session.Property("%WINDIR%")
var appcmd = windir + "\\system32\\inetsrv\\appcmd.exe list sites";
var oExec = shell.Exec(appcmd);
var sites = [];
while (!oExec.StdOut.AtEndOfStream) {
var oneLine = oExec.StdOut.ReadLine();
var line = ParseOneLine(oneLine);
LogMessage(" site: " + line.name);
sites.push(line);
}
return sites;
}
This works, but it briefly pops a visible console window, which then disappears. Doesn't look very polished. I think I can avoid the console window by using shell.Run() instead of shell.Exec(). But shell.Run() doesn't give access to the stdout, so I would have to redirect the output to a temporary file, then read the output. I haven't tried that yet. That may introduce some security issues; I'll have to see.
Related:
Where and how should my CustomAction create and read a temporary file?
Yes, you can run appcmd from the custom action the same way you do any custom action which runs exe. First off, you should author a DirectorySearch/FileSearch elements to find the full path to the executable. Next, add a custom action with ExeCommand attribute. You're probably trying to get feedback from a user, so leave it immediate. Also, think about using QuietExec in order not to show console window to your users.
By the way, if my guess is correct, you're trying to do something like this. Hope this helps.