如何在Windows脚本宿主中使用jQuery? [英] How do I use jQuery in Windows Script Host?

查看:83
本文介绍了如何在Windows脚本宿主中使用jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一些需要解析包含HTML片段的文件的代码。似乎jQuery对此非常有用,但是当我尝试将jQuery加载到WScript或CScript之类的东西时,由于jQuery对窗口对象的许多引用,它会抛出错误。

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.

在没有浏览器的代码中使用jQuery有什么实际的方法?

What practical way is there to use jQuery in code that runs without a browser?

更新:为了回应评论,我已成功编写JavaScript代码,使用 new ActiveXObject('Scripting.FileSystemObject'); 来读取文件内容。我知道ActiveX是邪恶的,但这只是一个内部项目,用于从包含HTML片段的一些文件中获取一些数据并进入适当的数据库。

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

})();

更新:我也在jQuery论坛上发布了这个问题:< a href =http://forum.jquery.com/topic/how-to-use-jquery-without-a-browser#14737000003719577 =nofollow> http://forum.jquery.com/topic/怎么用-jquery-没有浏览器#14737000003719577

Update: I posted this question on the jQuery forums as well: http://forum.jquery.com/topic/how-to-use-jquery-without-a-browser#14737000003719577

推荐答案

继续你的代码,你可以使用Windows脚本宿主创建一个IE实例,将你的html文件加载到实例中,动态地将jQuery附加到已加载的页面,然后从中添加脚本。

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.

这个适用于IE8的IE8,但我知道Windows 7 / IE9中存在一些安全问题。如果您遇到问题,可以尝试降低安全设置

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

这篇关于如何在Windows脚本宿主中使用jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆