導航:首頁 > 文件教程 > 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文件相關的資料

熱點內容
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
匯川am系列用什麼編程 瀏覽:41
筆記本win10我的電腦在哪裡打開攝像頭 瀏覽:827

友情鏈接