DW中怎么编写JavaScript脚本语言?

JavaScript011

DW中怎么编写JavaScript脚本语言?,第1张

在任何IDE和编辑器中都可以编写JS前端程序,而我们只需要注意注意JS语言的规范就行了。

目前在HTML文件中,运性JS有两种方式,一种是内嵌代码;另一种是编写单独的JS文件,然后引用。

第一种:内嵌代码

就是在<script type="text/javascript"></script>中直接编写代码,例如:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html charset=utf-8" />

<title>首页/title>

    <script type="text/javascript">

        alert("我是运性结果")

    </script>

</head>

<body>

</body>

</html>

第二种:外部引用

在<script>标签上添加一个src属性指向文件地址。

例如我们在user.js文件中写上:

alert("我是运性结果")

然后在HTML中引用:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html charset=utf-8" />

<title>首页</title>

    <script type="text/javascript" src="user.js">

</head>

<body>

</body>

</html>

上面两种的运性结果一样,都是在浏览器中弹出一个提示框

在编写xshell脚本的过程中用到最多的就是自动输入,自动捕获,延时等语句

自动输入

以自动输入xyz为例

自动输入的语句:xsh.Screen.Send("xyz")

当然,如果你输入的是一条命令,还需要下面这一行输入回车

输入回车的语句:xsh.Screen.Send(String.fromCharCode(13))

自动捕获

以linux系统为例,一般程序执行的打印数据位于倒数第二行,如下图所示

/* 字符串处理 */

var ScreenRow, ReadLine, Items

/* 读取倒数第二行,长度为40个字符 */

ScreenRow = xsh.Screen.CurrentRow - 1

ReadLine = xsh.Screen.Get(ScreenRow, 1, ScreenRow, 40)

延时

以等待1s为例

延时语句:xsh.Session.Sleep(1000)

其他

打开新会话:xsh.Session.Open(string)

对话框提醒:xsh.Dialog.MsgBox(string)

设置日志路径:xsh.Session.LogFilePath = string

开始记录日志:xsh.Session.StartLog()

清屏函数:xsh.Screen.Clear()

等待输入:xsh.Screen.WaitForString(string)

示例

本文以一个自动测试脚本为例,定时向/tmp/test文件写入数据,然后回读打印,截获回读打印的值进行分析

/* 测试函数 /

function test()

{

/ 发送echo 112233 >/tmp/testfile */

xsh.Screen.Send("echo 112233 >/tmp/testfile")

xsh.Screen.Send(String.fromCharCode(13))

}

/* 主函数 /

function Main()

{

/ 打开会话,根据实际的会话路径修改 */

xsh.Session.Open("C:\Users\Administrator\Documents\NetSarang Computer\6\Xshell\Sessions\ubuntu.xsh")

xsh.Screen.Synchronous = true

// xsh.Screen.WaitForString("start")

// xsh.Screen.Clear()

}

运行脚本的操作:

在编写xshell脚本的过程中用到最多的就是自动输入,自动捕获,延时等语句

自动输入

以自动输入xyz为例

自动输入的语句:xsh.Screen.Send("xyz")

当然,如果你输入的是一条命令,还需要下面这一行输入回车

输入回车的语句:xsh.Screen.Send(String.fromCharCode(13))

自动捕获

以linux系统为例,一般程序执行的打印数据位于倒数第二行,如下图所示

/* 字符串处理 */

var ScreenRow, ReadLine, Items

/* 读取倒数第二行,长度为40个字符 */

ScreenRow = xsh.Screen.CurrentRow - 1

ReadLine = xsh.Screen.Get(ScreenRow, 1, ScreenRow, 40)

延时

以等待1s为例

延时语句:xsh.Session.Sleep(1000)

其他

打开新会话:xsh.Session.Open(string)

对话框提醒:xsh.Dialog.MsgBox(string)

设置日志路径:xsh.Session.LogFilePath = string

开始记录日志:xsh.Session.StartLog()

清屏函数:xsh.Screen.Clear()

等待输入:xsh.Screen.WaitForString(string)

示例

本文以一个自动测试脚本为例,定时向/tmp/test文件写入数据,然后回读打印,截获回读打印的值进行分析

/* 测试函数 /

function test()

{

/ 发送echo 112233 >/tmp/testfile */

xsh.Screen.Send("echo 112233 >/tmp/testfile")

xsh.Screen.Send(String.fromCharCode(13))

}

/* 主函数 /

function Main()

{

/ 打开会话,根据实际的会话路径修改 */

xsh.Session.Open("C:\Users\Administrator\Documents\NetSarang Computer\6\Xshell\Sessions\ubuntu.xsh")

xsh.Screen.Synchronous = true

// xsh.Screen.WaitForString("start")

// xsh.Screen.Clear()

}

运行脚本的操作:

实际执行结果如下: