比如javascript的内容为:
<script language=javascript>
var arrData = new Array();
arrData[0] = 0;
arrData[1] = 1;
</script>
delphi程序如下(使用webbrowser控件):
function GetParam(scontent, sStart, sEnd: string): string;
var
pnIndex: Integer;
begin
if (Pos(sStart, scontent) = 0) or (Pos(sEnd, scontent) = 0) then
begin
result := '';
Exit;
end;
result := Copy(scontent, Pos(sStart, scontent) + Length(sStart), Pos(sEnd,
scontent) - Length(sStart) - Pos(sStart, scontent));
end;
procedure TFm_main.WebBrowser1DocumentComplete(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant);
var
hform: IHTMLFormelement;
hdoc: ihtmldocument2;
strResult,strTemp: string;
arrData:array[0..999] of string;//假设javascript中的数组元素不会超过1000
i:integer;
begin
strTemp:='';
if Assigned(WebBrowser1) then
begin
hdoc := WebBrowser1.document as ihtmldocument2;
hform := hdoc.Forms.item(0, 0) as IHTMLFormElement;
try
strResult := StringReplace(hdoc.body.outerHTML, ' ', '', [rfReplaceAll]);//替换所有空格
{然后再解析strResult的内容,逐个把数组的内容读到.}
for i:=0 to 999 do
begin
strTemp:= GetParam(strResult,'arrData['+IntToStr(i)+']=',';');
if trim(strTemp) = '' then
break;
arrData := strTemp;
strResult := copy(strResult,pos('arrData['+IntToStr(i)+']=',length(strResult)));
end
except
end;
end;
end;
程序在实现还不够完善,不过提供了一些思路,希望有用.