網頁轉向
一、按下按鈕轉向
首頁:index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
function redirectToPage1() {
google.script.run.withSuccessHandler(updatePageContent).includePage1();
}
function updatePageContent(content) {
document.open();
document.write(content);
document.close();
}
</script>
</head>
<body>
<h1>Home - index.html</h1>
<button onclick="redirectToPage1()">Redirect to Page 1</button>
</body>
</html>
轉向:page1.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<h1>Page 1</h1>
<p>這是Page 1的內容。</p>
</body>
</html>
Code.gs
function doGet() {
return HtmlService.createTemplateFromFile('index').evaluate();
}
function includePage1() {
var page1Content = HtmlService.createTemplateFromFile('page1').getRawContent();
return page1Content;
}
說明
按下按鈕,呼叫JavaScript的redirectToPage1()函式。
以google.script.run.withSuccessHandler,呼叫GAS函式includePage1。
以 HtmlService.createTemplateFromFile('Page1').getRawContent()方法,取得網頁內容後返回。
執行 JavaScript函式 updatePageContent(),以 document物件處理page1.html網頁。