web-view is a container that directly integrates the web page content into the UI of your atomic service, so that users can browse a web page without leaving the atomic service, thereby providing seamless browsing experience for users.
Install the JavaScript SDK. For details, please refer to JavaScript SDK.
For details about the corresponding API capabilities, please refer to web-view.
How to Obtain
Integrate the JavaScript SDK to your project in NPM mode.
How to Install
Open the cmd window in the target path and run the following npm command to obtain the corresponding JavaScript file:
npm install @atomicservice/ascf-web-sdk
How to Use
ES6
import has from '@atomicservice/ascf-web-sdk';
has.ascfweb.getEnv((res) => {
console.info('getEnv success', res);
}); umd
<script src="../dist/ascf-web-sdk.umd.js"></script>
<script>
has.ascfweb.getEnv((res) => {
console.info('getEnv success', res);
});
</script> A success message displayed in the command output indicates that the SDK has been successfully installed and you can use the web-view component.
Copy the ascf-web-sdk.umd.js file in the npm installation package to the static resource directory of the project, and change the value of script src in the preceding example to the corresponding path.
User-Agent
When web-view is used to access the server, ASCF/App ID is appended to the end of the User-Agent string. The server can use this identifier to check whether the page is loaded within ASCF.
Example:
Bidirectional communication description
The HTML5 page embedded in web-view sends a message to the atomic service through the has.ascfweb.postMessage API. The atomic service receives the message through the bindmessage attribute.
The atomic service sends a message to the HTML5 page embedded in web-view through the WebViewContext.postMessage API. The HTML5 page receives the message through the has.ascfweb.onMessage API.
Example:
Scenario 1: The HTML5 page sends a message to the atomic service.
Call the JavaScript SDK API on the HTML5 page.
After the JavaScript SDK is integrated into the HTML5 page on the server, directly call the has.ascfweb.postMessage API.
To send real-time messages, pass { mode: 'realtime' } in options.
index.html:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PostMessage H5</title>
<script src="ascf-web-sdk.umd.js"></script>
</head>
<body>
<h1>H5 - has.ascfweb.postMessage</h1>
<button type="button" class="button" onclick="postMessage1();">Send a string message from the HTML5 page to the atomic service</button>
<button type="button" class="button" onclick="postMessage2();">Send an object message from the HTML5 page to the atomic service</button>
</body>
<script type="text/javascript">
// Send a string message.
function postMessage1() {
has.ascfweb.postMessage({
data: 'H5 string to Js',
options: { mode: 'realtime' },
success: () => {
console.info('postMessage success');
},
fail: (err) => {
console.error('postMessage fail:', err.message);
},
complete: (res) => {
console.info('postMessage complete:', res);
}
});
}
// Send an object message.
function postMessage2() {
has.ascfweb.postMessage({
data: { name: 'H5 object to Js' },
options: { mode: 'realtime' },
success: () => {
console.info('postMessage success');
},
fail: (err) => {
console.error('postMessage fail:', err.message);
},
complete: (res) => {
console.info('postMessage complete:', res);
}
});
}
</script>
</html> Load the HTML5 page in the HXML file.
Use the web-view component to set url and load the HTML5 page on the server. Bind a callback for receiving messages to the bindmessage attribute.
xxx.hxml:
<web-view src="{{ url }}" bindmessage="onWebviewMessage"></web-view> Process the message sent from the HTML5 page in the JavaScript file.
xxx.js:
Page({
data: {
url: 'http://www.example.com/example.html' // Replace the example URL with the actual URL.
},
onWebviewMessage(e) {
// Receive the message sent from the HTML5 page.
console.info('onWebviewMessage:', e.detail);
}
}); Scenario 2: The atomic service sends a message to the HTML5 page.
Call the JavaScript SDK API on the HTML5 page.
After the JavaScript SDK is integrated into the HTML5 page on the server, directly call the has.ascfweb.onMessage API. Receive the message through onMessage.
index.html:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PostMessage H5</title>
<script src="ascf-web-sdk.umd.js"></script>
</head>
<body>
<h1>H5 - has.ascfweb.onMessage</h1>
</body>
<script type="text/javascript">
// Listen for messages sent from the JavaScript file.
has.ascfweb.onMessage((data) => {
console.info('onMessage: Message received by the HTML5 page from atomic service:', data);
});
</script>
</html> Load the HTML5 page in the HXML file.
Use the web-view component to set url and load the HTML5 page on the server.
xxx.hxml:
<view class="menu-list">
<view class="web-view-class">
<web-view src="{{ url }}" bindmessage="onWebviewMessage" class="web-view-class"></web-view>
</view>
<button bindtap="sendString" type="primary">Send a string message</button>
<button bindtap="sendSimpleObject" type="primary">Send a simple object message</button>
</view> xxx.css:
.web-view-class {
height: 400px;
}
.menu-list {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 12px;
padding: 16px;
} Send a message to the HTML5 page in the JavaScript file.
Use the has.createWebViewContext() method to obtain the control instance of the web-view component and call the postMessage method of the instance to send a message.
xxx.js:
Page({
data: {
url: 'http://www.example.com/example.html' // Replace the example URL with the actual URL.
},
sendString() {
// Send a string message to the HTML5 page.
const ctx = has.createWebViewContext();
ctx.postMessage({
data: 'Js string message to H5',
success: () => {
console.info('postMessage success');
},
fail: (err) => {
console.error('postMessage fail:', err);
},
complete: (res) => {
console.info('postMessage complete:', res);
}
});
},
sendSimpleObject() {
// Send an object message to the HTML5 page.
const ctx = has.createWebViewContext();
ctx.postMessage({
data: {
msg: 'JS object message to H5',
version: '1.0'
},
success: () => {
console.info('postMessage success');
},
fail: (err) => {
console.error('postMessage fail:', err);
},
complete: (res) => {
console.info('postMessage complete:', res);
}
});
}
});