导航:首页 > 文件教程 > delphi读写js文件

delphi读写js文件

发布时间:2024-07-02 07:01:42

① 如何通过js或者vb调用delphi的com组件

MsgBox data data = info.GetData(key, data) key = info.Data //通过属性MsgBox key</script></html>2、js<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>TestAxObj</title></head><body style="background-color:whitesmoke; margin:5px"> <form method="post" action=""> <object id="tc1" width="200px" height="20px" style="background-color:red;" classid="CLSID:65E6BE73-2814-4B52-850A-939786D009BC"> </object> <script type="text/javascript" language=javascript> var tc1 = document.getElementById("tc1"); var s = "test"; var out = ""; var data1 = ""; tc1.GetData(s, out); data1 = tc1.Data;//属性的方式 alert("属性" + data1); s = tc1.TestData(data1); alert(s);//返回值方式</script></body></html>这里的调用是针对于上篇文章的com编写方法写的调用脚本,大家可以参考来看。其实,这里还有一个问题是没有解决的,本来想通过js脚本实现,delphi中var类型的参数的功能,实现一个数据的改变,例如在 var out = ""; tc1.GetData(s, out);中想实现out值的更改,但是没能实现,所以将out的值传递给其属性,然后访问属性获得结果。 目前js可以使用的方法是通过属性和返回值的方式来处理类似的情况。特意说明在vb中没有这种限制,可以使用这种方式的值的改变。

② 濡備綍鐢―elphi鍦═WebBrowser涓璋冪敤JavaScript

鍦═WebBrowser涓璋冪敤鑴氭湰鐨勫姙娉曟槸璋冪敤Html鏂囨。鐩稿叧鐨勫硅薄绐楀彛涓鐨別xecScript鏂规硶銆傝嚦浜庝粈涔堟槸鍜孒tml Document鐩稿叧鐨勫硅薄绐楀彛锛屽悗闈㈢殑浠g爜涓鐢ㄥ埌鐨処HTMLWindow2灏辨槸銆俥xecScript鍑芥暟瀹氫箟濡備笅锛

function execScript(const code: WideString; const language: WideString): OleVariant;

鍙傛暟code鏄涓涓鑴氭湰鍑芥暟鐨勫畬鏁磋皟鐢ㄥ舰寮忕殑瀛楃︿覆锛屼緥濡傛湁涓涓狫avaScript鍑芥暟瀹氫箟涓猴細
function foo(param1)锛屽垯 code="foo(param1)"銆

鍙傛暟language琛ㄧず鑴氭湰鐨勭被鍨嬶紝渚嬪 language="JavaScript"

棣栧厛锛岃幏鍙栨祻瑙堝櫒缁勪欢鐨勬枃妗e硅薄锛涚劧鍚庨氳繃璇ユ枃妗e硅薄鐨凱arentWindow灞炴ф潵鑾峰彇绐楀彛瀵硅薄銆傛渶鍚庨氳繃璇ョ獥鍙e硅薄鏉ヨ皟鐢╡xecScript鍗冲彲銆備笅闈㈠氨缁欏嚭涓涓绠鍗曠殑瀹炵幇绀轰緥銆

瀹炵幇绀轰緥

uses
MSHTML;

procere TForm1.CallFoo(S: string; I: Integer);
{ Calls JavaScript Foo() function }
var
Doc: IHTMLDocument2; // current HTML document
HTMLWindow: IHTMLWindow2; // parent window of current HTML document
JSFn: string; // stores JavaScipt function call
begin
// Get reference to current document
Doc := WebBrowser1.Document as IHTMLDocument2;
if not Assigned(Doc) then
Exit;
// Get parent window of current document
HTMLWindow := Doc.parentWindow;
if not Assigned(HTMLWindow) then
Exit;
// Run JavaScript
try
JSFn := Format('Foo(''%s'',%d)', [S, I]); // build function call
HTMLWindow.execScript(JSFn, 'JavaScript'); // execute function
except
// handle exception in case JavaScript fails to run
end;
end;

瀹炰緥婕旂ず

鏁翠釜瀹炰緥鍖呮嫭涓ら儴鍒嗭細

缃戦〉鏂囦欢test.html锛氭枃浠跺唴鏈変竴涓狫avaScript鍑芥暟SetFont銆傝ュ嚱鏁伴氳繃涓嬫媺妗嗘潵閫夋嫨瀛椾綋锛岀劧鍚庣偣鍑烩漵et font鈥滄寜閽鏉ユ敼鍙橀〉闈㈠瓧浣撱
Delphi绔绋嬪簭锛氶氳繃TWebbrowser鏉ユ樉绀洪〉闈锛屽苟婕旂ず濡備綍璋冪敤椤甸潰鍐呯殑Javascript鍑芥暟銆

Test.html:

<html>
<head>
<title> Demo for call Javascript from Delphi
</title>
<script type="text/javascript">
<!--
function SetFont(fontname)
{
document.body.style.fontFamily = fontname;
}
-->
</script>
</head>

<body>
demo of calling Javascript from Delphi
<form>
<select size=1 name="selfont">
<option value="Verdana" selected>Verdana</option>
<option value="Arial">Arial</option>
<option value="Courier New">Courier New</option>
<option value="Tahoma">Tahoma</option>
</select>
<input type="button" value="set font" name="btn1"
onclick="SetFont(selfont.value)">
</form>
</body>
</html>

Delphi鎺у埗Javascript

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, SHDocVw, StdCtrls, Mshtml;

type
TForm1 = class(TForm)
btnCallJS: TButton;
cmbFonts: TComboBox;
WebNav: TWebBrowser;
procere FormShow(Sender: TObject);
procere WebNavDocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
procere btnCallJSClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procere TForm1.FormShow(Sender: TObject);
begin
// Disable button
btnCallJS.Enabled := false;
// Load the Html page
WebNav.Navigate(ExtractFilepath(Application.ExeName)
+'test.html');
end;

procere TForm1.WebNavDocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
begin
// When complete loading Html page, enable button
btnCallJS.Enabled := true;
end;

// Call the Javascript in Html page
procere TForm1.btnCallJSClick(Sender: TObject);
var
// current Html document
Doc : IHtmlDocument2;
// parent window of current Html document
HtmlWnd : IHtmlWindow2;
// Javascript function name including arguments
JsFnc : string;
begin
// Get reference to current document
Doc := WebNav.Document as IHtmlDocument2;
if not assigned(Doc) then
exit;
// Get parent window of current Html document
HtmlWnd := Doc.parentWindow;
if not assigned(HtmlWnd) then
exit;
// Run Javascript
try
JsFnc := 'SetFont(''' + trim(cmbFonts.Text) + ''')';
HtmlWnd.execScript(JsFnc, 'JavaScript');
except
Showmessage('Call JavaScript failed!');
end;
end;

end.

③ DELPHI如何调用页面里的JS

var
js: OleVariant;
str: string;
begin
js := CreateOleObject('ScriptControl'); // 创建组件
js.Language := 'JavaScript'; // 指定组件所使用的语言,也可以是VBScript
js.AddCode(Memo1.Text); // 加入JS代码,如上面的代码
str := js.Eval('getTime()');// getTime() function from JS
ShowMessage(str);

end;

Memo1.text内容

function getTime(){
var d, s = "";
var c = "";
d = new Date();
s += d.getYear()+c;
s += (d.getMonth() + 1) + c;
s += d.getDate() + c;
s += d.getHours() + c;
s += d.getMinutes() + c;
s += d.getSeconds() + c;
s += d.getMilliseconds();
return s;
}

④ delphi怎么得到下面的json数据中的多个节点值

Delphi下解析json可以去下载一个superobject单元,关于这个单元的使用可以网络一下,很好用的

Delphi/Pascal code?

// Sample 2: how to get child type and subobject fields
//
// Leonid Koninin, 02/03/2007
program sample2;
{$APPTYPE CONSOLE}
uses SysUtils, uLkJSON in 'uLkJSON.pas';
var JSon, XJSon :TlkJSONobject; JSonNode :TlkJSONString; JSonNodeList :TlkJSONlist; Str :String; i :Integer;
begin
Str := '{"string1":"one", "string2":"two", '
+'"childobject" : {"objstr1" :"Oone", "objstr2" :"Otwo"},'
+'"childobject2":[{"obj2str1":"2one"},{"obj2str1":"2two"}]}'; writeln(Str);

JSon := TlkJSON.ParseText(Str) as TlkJSONobject; // restore object (parse text)

if not assigned(JSon) then begin // how to obtain type of child
writeln('error: xs not assigned!'); readln; //exit;
end else begin //Field[] is 方式判断类型
if JSon.Field['childobject'] is TlkJSONString then writeln('type: xs is string!'); //string
if JSon.Field['childobject'] is TlkJSONlist then writeln('type: xs is list!'); //list 多个Json子节点
if JSon.Field['childobject'] is TlkJSONobject then writeln('type: xs is object!'); //Oject 单个json子节点

//以下类型,实际不常用
if JSon.Field['childobject'] is TlkJSONnumber then writeln('type: xs is number!'); //数字 value前后不加引号
if JSon.Field['childobject'] is TlkJSONboolean then writeln('type: xs is boolean!'); //boolean
if JSon.Field['childobject'] is TlkJSONnull then writeln('type: xs is null!'); //Null 改为空格值
end;

//Filed[].SelfType 方式判断类型
case JSon.Field['childobject'].SelfType of //the other way (0.93+)
jsString :writeln('other type: xs is string');
jsObject :writeln('other type: xs is object');
jsList :writeln('other type: xs is list');

jsNumber :writeln('other type: xs is number');
jsBoolean :writeln('other type: xs is boolean');
jsNull :writeln('other type: xs is null');
jsBase :writeln('other type: xs is base');
end;
writeln('self-type name: ', JSon.Field['childobject'].SelfTypeName);

XJSon :=JSon.Field['childobject'] as TlkJSONobject; //JSON中,有下级节点的,不像xml那样称为节点对象,仍称为JSOn对象

//Field[] as方式取值,完美
JSonNode :=XJSon.Field['objstr1'] as TlkJSONString; //we know what xs chilren are strings
writeln(JSonNode.value);

JSonNode :=XJSon.Field['objstr2'] as TlkJSONstring;
writeln(JSonNode.value);

//getstring,快速
writeln(XJSon.getString('objstr1')); //new v0.99 +syntax!
writeln(XJSon.getString('objstr2')); readln;

JSon.Free;
end.

阅读全文

与delphi读写js文件相关的资料

热点内容
网站怎么申请认证 浏览:676
短信apk是什么文件 浏览:940
app官方下载在哪里 浏览:511
iphone5s改名字 浏览:772
win10文件夹打开一直闪跳 浏览:208
win10摄像头不能拍照 浏览:56
云阳数控编程培训哪里学 浏览:519
文件的存放路径怎么改 浏览:583
cad字体文件如何导出 浏览:236
cs16需要cdkey哪个文件里 浏览:817
如何让另一个表格的数据关联 浏览:368
来自app的快捷指令是怎么有的 浏览:844
保留文件的原始文字图片 浏览:385
国外网站的店怎么看呢 浏览:56
ps入门必备文件 浏览:348
以前的相亲网站怎么没有了 浏览:15
苹果6耳机听歌有滋滋声 浏览:768
怎么彻底删除linux文件 浏览:379
编程中字体的颜色是什么意思 浏览:534
网站关键词多少个字符 浏览:917

友情链接