Cannot simply force focus on text input in stand-alone Google App using HtmlService?

白昼怎懂夜的黑 提交于 2019-11-28 10:13:08

问题


I want to script a click on an input box.

Here is an example where the focus() should do just that, but it doesn't! Why?

Code.gs:

function doGet(e) {

  return HtmlService.createHtmlOutputFromFile('myFile');

}  

myFile.html:

<input type="text" id="new" onchange="adding(this)"/>

<div id="data"></div>

<script>

   document.getElementById('new').focus();

   function adding(a){

      document.getElementById('data').innerHTML += a.value;

      a.value = '';
   }

</script>

I have also tried without success putting the focus() in its own function and having a body element whose onload calls that function.

What DOES work is having a button whose onclick calls that function, so focus() does eventually become active. Is there some other event I can use to trigger it?

I am using a Chromebook. Could that be the problem?


回答1:


This is an intentional security decision in Caja. Certain functions that are prone (across the web) to serious malicious misuse, such as submit() and focus(), can only be executed while in the context of a user-initiated event (such as a button click).



来源:https://stackoverflow.com/questions/15387457/cannot-simply-force-focus-on-text-input-in-stand-alone-google-app-using-htmlserv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!