Difference between revisions of "JS.eval"
From GiderosMobile
m (Text replacement - "</source>" to "</syntaxhighlight>") |
|||
| (7 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
| − | ''' | + | '''Available since:''' Gideros 2016.10<br/> |
| − | ''' | + | '''Class:''' [[JS]]<br/> |
| − | === | + | |
| − | + | === Description === | |
| − | < | + | Executes arbitrary JavaScript code on HTML5 platform. |
| − | + | <syntaxhighlight lang="lua"> | |
| − | </ | + | JS.eval(code) |
| − | === | + | </syntaxhighlight> |
| − | ''' | + | |
| + | === Parameters === | ||
| + | '''code''': (string) JavaScript code to execute<br/> | ||
| + | |||
| + | === Examples === | ||
| + | '''Get/set''' | ||
| + | <syntaxhighlight lang="lua"> | ||
| + | JS.eval(localStorage.setItem('name', 'John')) | ||
| + | print(JS.eval(localStorage.getItem('name')) | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | |||
| + | '''Opens an url''' | ||
| + | <syntaxhighlight lang="lua"> | ||
| + | JS.eval([[ | ||
| + | window.open('my.url') | ||
| + | ]]) | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | |||
| + | '''In your html5 exports use this to detect if you are in light or dark mode - to change your app or game colors to suit...''' | ||
| + | <syntaxhighlight lang="lua"> | ||
| + | sysDark=-1 | ||
| + | |||
| + | -- device={application:getDeviceInfo()} | ||
| + | -- device[1]=="Web" | ||
| + | -- only do the next line if html5 export | ||
| + | if JS.eval("window.matchMedia('(prefers-color-scheme:dark)').matches")=="true" then sysDark=1 | ||
| + | else sysDark=0 | ||
| + | end | ||
| + | </syntaxhighlight> | ||
| + | You will end up with sysDark being -1 for 'don't know', 0 for 'light' and 1 for 'dark'. | ||
| + | |||
| + | |||
| + | '''Sometimes you may not want the user to leave the game without giving a warning that they will lose information, here is code that will allow you to do this on html5 exports:''' | ||
| + | <syntaxhighlight lang="lua"> | ||
| + | if JS then | ||
| + | JS.eval([[ | ||
| + | window.saveWarning=false; | ||
| + | window.saveWarningListener = (e) => { | ||
| + | if (window.saveWarning==true) { | ||
| + | e.preventDefault(); | ||
| + | e.returnValue=''; | ||
| + | } | ||
| + | }; | ||
| + | window.addEventListener('beforeunload', saveWarningListener); | ||
| + | ]]) | ||
| + | end | ||
| + | |||
| + | function saveWarning(f) | ||
| + | if JS then | ||
| + | if f then JS.eval("window.saveWarning=true;") | ||
| + | else JS.eval("window.saveWarning=false;") | ||
| + | end | ||
| + | end | ||
| + | end | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | {{JS}} | ||
Latest revision as of 14:31, 13 July 2023
Available since: Gideros 2016.10
Class: JS
Description
Executes arbitrary JavaScript code on HTML5 platform.
JS.eval(code)
Parameters
code: (string) JavaScript code to execute
Examples
Get/set
JS.eval(localStorage.setItem('name', 'John'))
print(JS.eval(localStorage.getItem('name'))
Opens an url
JS.eval([[
window.open('my.url')
]])
In your html5 exports use this to detect if you are in light or dark mode - to change your app or game colors to suit...
sysDark=-1
-- device={application:getDeviceInfo()}
-- device[1]=="Web"
-- only do the next line if html5 export
if JS.eval("window.matchMedia('(prefers-color-scheme:dark)').matches")=="true" then sysDark=1
else sysDark=0
end
You will end up with sysDark being -1 for 'don't know', 0 for 'light' and 1 for 'dark'.
Sometimes you may not want the user to leave the game without giving a warning that they will lose information, here is code that will allow you to do this on html5 exports:
if JS then
JS.eval([[
window.saveWarning=false;
window.saveWarningListener = (e) => {
if (window.saveWarning==true) {
e.preventDefault();
e.returnValue='';
}
};
window.addEventListener('beforeunload', saveWarningListener);
]])
end
function saveWarning(f)
if JS then
if f then JS.eval("window.saveWarning=true;")
else JS.eval("window.saveWarning=false;")
end
end
end