Difference between revisions of "JS.eval"

From GiderosMobile
m
(removed language stuff and added example from sinistersoft)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
<languages/>
+
'''Available since:''' Gideros 2016.10<br/>
'''<translate>Available since</translate>:''' in development<br/>
+
'''Class:''' [[JS]]<br/>
'''<translate>Class</translate>:''' [[Special:MyLanguage/JS|JS]]<br/>
 
  
=== <translate>Description</translate> ===
+
=== Description ===
<translate>Executes arbitrary Javascript code on HTML5 platform.</translate>
+
Executes arbitrary JavaScript code on HTML5 platform.
 
<source lang="lua">
 
<source lang="lua">
 
JS.eval(code)
 
JS.eval(code)
 
</source>
 
</source>
  
=== <translate>Parameters</translate> ===
+
=== Parameters ===
'''code''': (string) <translate>JavaScript code to execute</translate><br/>
+
'''code''': (string) JavaScript code to execute<br/>
 +
 
 +
=== Example ===
 +
'''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:'''
 +
<source 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
 +
</source>
  
 
{{JS}}
 
{{JS}}

Revision as of 04:26, 8 September 2021

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

Example

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