comjs函数(js com)
更新:<time datetime="2022-11-14 13:35">2022-11-14 13:35</time>
本文目录一览:
1、js中的常用函数
2、com组件如何异步调用js函数?
3、JS调用COM组件是什么意思
js中的常用函数
下面介绍全页面刷新方法:有时候可能会用到
window.location.reload()
刷新当前页面.parent.location.reload()
刷新父亲对象(用于框架)opener.location.reload()
刷新父窗口对象(用于单开窗口)top.location.reload()
刷新最顶端对象(用于多开窗口) 下面再介绍一些javascript基本函数
document.write("");
为输出语句- js中的注释为
//
- 传统的html文档顺序是:
document -> html -> (head, body)
- 一个浏览器窗口中的dom顺序是:
window -> (navigator, screen, history, location, document)
- 得到表单中元素的名称和值:
document.getElementById("表单中元素的id号").name
(或value
) - 一个小写转大写的js:
document.getElementById("output").value = document.getElementById("input").value.toUpperCase();
- js中的值类型:
string
,number
,boolean
,null
,object
,function
- js中的字符型转换成数值型:
parseInt()
,parseFloat()
- js中的数字转换成字符型:
(""+变量)
- js中的取字符串长度是:
(length)
- js中的字符与字符相连接使用
+
号。 - js中的比较操作符有:
==
等于,!=
不等于,>
,<
,>=
,<=
。 - js中声明变量使用:
var
来进行声明 - js中的判断语句结构:
if(condition){}else{}
- js中的循环结构:
for([initial expression];[condition];[update expression]) {inside loop}
- 循环中止的命令是:
break
- js中的函数定义:
function functionname([parameter],…){statement[s]}
- 当文件中出现多个form表单时,可以用
document.forms[0]
,document.forms[1]
来代替。 - 窗口:打开窗口
window.open()
,关闭一个窗口:window.close()
,窗口本身:self
- 状态栏的设置:
window.status="字符";
- 弹出提示信息:
window.alert("字符");
- 弹出确认框:
window.confirm();
- 弹出输入提示框:
window.prompt();
- 指定当前显示链接的位置:
window.location.href="url"
- 取出窗体中的所有表单的数量:
document.forms.length
- 关闭文档的输出流:
document.close();
- 字符串追加连接符:
+=
- 创建一个文档元素:
document.createElement()
,document.createTextNode()
- 得到元素的方法:
document.getElementById()
- 引用一个文件式的js:
- 指定在不支持脚本的浏览器显示的html:
- 当超链和onclick事件都有时,则老版本的浏览器转向a.html,否则转向b.html。例: dfsadf
- js 的内建对象有:
Array
,Boolean
,Date
,Error
,EvalError
,Function
,Math
,Number
,Object
,RangeError
,ReferenceError
,RegExp
,String
,SyntaxError
,TypeError
,URIError
- js中的换行:
\n
- 窗口全屏大小:
function fullscreen() {
this.moveTo(0, 0);
this.outerWidth = screen.availWidth;
this.outerHeight = screen.availHeight;
}
window.maximize = fullscreen;
- js中的
all
代表其下层的全部元素 - js中的焦点顺序:
document.getElementById("表单元素").tabIndex = 1
innerHTML
的值是表单元素的值:如how are you
,则innerHTML
的值就是:how are you
innerText
的值和上面的一样,只不过不会把em
这种标记显示出来。contentEditable
可设置元素是否可被修改,isContentEditable
返回是否可修改的状态。isDisabled
判断是否为禁止状态,disabled
设置禁止状态length
取得长度,返回整型数值addBehavior()
是一种js调用的外部函数文件其扩展名为.htc
window.focus()
使当前的窗口在所有窗口之前。blur()
指失去焦点,与focus()
相反。select()
指元素为选中状态。- 防止用户对文本框中输入文本:
onfocus="this.blur()"
- 取出该元素在页面中出现的数量:
document.all.tags("div(或其它html标记符)").length
- js中分为两种窗体输出:模态和非模态:
window.showModalDialog()
,window.showModeless()
- 状态栏文字的设置:
window.status='文字'
,默认的状态栏文字设置:window.defaultstatus = '文字.';
- 添加到收藏夹:
external.addFavorite("","Google");
- js中遇到脚本错误时不做任何操作:
window.onerror = doNothing;
指定错误句柄的语法为:window.onerror = handleError;
- js中指定当前打开窗口的父窗口:
window.opener
,支持opener.opener…
的多重继续。 - js中的
self
指的是当前的窗口 - js中状态栏显示内容:
window.status="内容"
- js中的
top
指的是框架集中最顶层的框架 - js中关闭当前的窗口:
window.close();
- js中提出是否确认的框:
if (confirm("Are you sure?")) {
alert("OK");
} else {
alert("Not OK");
}
- js中的窗口重定向:
window.navigate("");
- js中的打印:
window.print()
- js中的提示输入框:
window.prompt("message","defaultreply");
- js中的窗口滚动条:
window.scroll(x,y)
- js中的窗口滚动到位置:
window.scrollBy
- js中设置时间间隔:
setInterval("expr",msecdelay)
或setInterval(funcref,msecdelay)
或setTimeout
- js中的模态显示在IE4+行,在NN中不行:
showModalDialog("url"[,arguments][,features]);
- js中的退出之前使用的句柄:
function verifyClose() {
event.returnValue = "We really like you and hope you will stay longer.";
}
window.onbeforeunload = verifyClose;
- 当窗体第一次调用时使用的文件句柄:
onload()
- 当窗体关闭时调用的文件句柄:
onunload()
window.location
的属性:protocol
(http:),hostname()
,port
(80),host()
,pathname
("/a/a.html"),hash
("#giantgizmo", 指跳转到相应的锚记),href
(全部的信息)window.location.reload()
刷新当前页面。parent.location.reload()
刷新父亲对象(用于框架)opener.location.reload()
刷新父窗口对象(用于单开窗口)top.location.reload()
刷新最顶端对象(用于多开窗口)window.history.back()
返回上一页,window.history.forward()
返回下一页,window.history.go(返回第几页,也可以使用访问过的url)
document.write()
不换行的输出,document.writeln()
换行输出document.body.nowrap=true;
防止链接文字折行。变量名.charAt(第几位)
,取该变量的第几位的字符。"abc".charCodeAt(第几个)
,返回第几个字符的ASCII码值。- 字符串连接:
string.concat(string2)
,或用+=
进行连接 变量.indexOf("字符",起始位置)
,返回第一个出现的位置(从0开始计算)string.lastIndexOf(searchString[,startIndex])
最后一次出现的位置。string.match(regExpression)
,判断字符是否匹配。string.replace(regExpression,replaceString)
替换现有字符串。string.split(分隔符)
返回一个数组存储值。string.substr(start[,length])
取从第几位到指定长度的字符串。string.toLowerCase()
使字符串全部变为小写。string.toUpperCase()
使全部字符变为大写。parseInt(string[,radix(代表进制)])
强制转换成整型。parseFloat(string[,radix])
强制转换成浮点型。isNaN(变量)
:测试是否为数值型。- 定义常量的关键字:
const
,定义变量的关键字:var
com组件如何异步调用js函数?
// 初始化创建线程
STDMETHODIMP CJSCallBack::Initialize(void) {
// TODO: 在此添加实现代码
DWORD dThreadId;
m_hThreadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CallJspFunctionThread, this, 0, dThreadId);
return S_OK;
}
// 线程函数
UINT CJSCallBack::CallJspFunctionThread(LPVOID param) {
CJSCallBack* pCallBack = (CJSCallBack*)param;
while (m_bRunThread) {
while (m_bCallJspFunc) {
pCallBack->CallJsFunction();
m_bCallJspFunc = FALSE;
}
Sleep(1);
}
return 0;
}
// 设置回调函数
STDMETHODIMP CJSCallBack::SetJsCallbackFunc(VARIANT scriptCallback) {
// TODO: 在此添加实现代码
if (scriptCallback.vt == VT_DISPATCH) {
m_spCallback = scriptCallback.pdispVal;
}
return S_OK;
}
// 调用回调函数
void CJSCallBack::CallJsFunction() {
CComVariant avarParams[1];
avarParams[0] = m_szDisplay; // 指定回调函数的参数
DISPPARAMS params = { avarParams, NULL, 1, 0 };
if (m_spCallback) {
HRESULT hr = m_spCallback->Invoke(0, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, ¶ms, NULL, NULL, NULL);
}
}
// 触发调用回调函数
STDMETHODIMP CJSCallBack::Print(BSTR bstrToPrint, LONG lWait) {
// TODO: 在此添加实现代码
_bstr_t bsPrint = bstrToPrint;
char* szPrint = bsPrint;
Sleep(500);
m_szDisplay = "Succeed to Print";
HRESULT hr = CallPrintFunction(szPrint);
if (FAILED(hr)) {
m_szDisplay = "Failed to Print";
}
switch (lWait) {
case 0:
CallJsFunction();
break;
case 1:
m_bCallJspFunc = TRUE;
Sleep(30);
break;
default:
break;
}
return S_OK;
}
function TestCallBack() {
var obj = new ActiveXObject("CallBackForJs.JSCallBack.1");
var result = obj.Initialize();
result = obj.SetJsCallbackFunc(printMsg);
obj.Print("abc",1);
obj.UnInitialize();
}
function printMsg(msg) {
alert(msg);
}
JS调用COM组件是什么意思
在js下面直接将function传给这个属性,com组件里面调用这个IDispatch的函数即可。 idl文件,注意onevent1这里。
import "oaidl.idl";
import "ocidl.idl";
[
object,
uuid(6A6140E8-9356-4FBD-B0FD-04508D336B17),
dual,
nonextensible,
pointer_default(unique)
]
interface IComClassTest : IDispatch {
[id(1)] HRESULT Method1(BSTR arg1);
[propget, id(2), bindable, displaybind] HRESULT onevent1([out, retval] VARIANT* pVal);
[propput, id(2), bindable, displaybind] HRESULT onevent1([in] VARIANT newVal);
};
[
uuid(97157E1C-637C-4962-B069-96E70F572A99),
version(1.0),
]
library ATLProject3Lib