Dear JS-Support,
we have found a bug concerning javascript named functions.
(We are not 100% sure it is a bug (probably) or not -but for sure to help others to eliminate spend hours..)
Description:
if your declare a javascript named function by ExecuteJavaScript, e.g.:
CSHTML5.Interop.ExecuteJavaScript(@"function myFunction(a, b) {
return a * b;
}
and try to invoke later, e.g.:
CSHTML5.Interop.ExecuteJavaScript(@"alert(myFunction(2,3))");
It will works in Simulator, but got exception in real browser: 'Uncaught ReferenceError: myFunction is not defined'
If you declare the javascript named functions in external js file (e.g.: someFunctions.js) and use the following pattern:
Page_Loaded () => LoadJavaScriptFile() => ExecuteJavaScript() later:
works as expected.
Full sample source code:
XAML:
<Page
x:Class="CSHTML5cacheEsJSfunctionTestPort14445.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:CSHTML5cacheEsJSfunctionTestPort14445"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Loaded="Page_Loaded">
<StackPanel>
<Button Content="test (the pre-defined) myFunction by CSHTML5.Interop.ExecuteJavaScript(...)" Click="Button_Click"/>
<Button Content="test (the pre-defined) myFunctionFromExternalJSfile by .LoadJavaScriptFile => .ExecuteJavaScript(...)" Click="Button_Click_1"/>
</StackPanel>
</Page>
C#:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace CSHTML5cacheEsJSfunctionTestPort14445
{
public partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
CSHTML5.Interop.ExecuteJavaScript(@"function myFunction(a, b) {
return a * b;
}
");
CSHTML5.Interop.LoadJavaScriptFile("ms-appx:///CSHTML5cacheEsJSfunctionTestPort14445/someFunctions.js");
}
private void Button_Click(object sender, RoutedEventArgs e)
{ // Simulator: working
// in real browser: 'Uncaught ReferenceError: myFunction is not defined'
CSHTML5.Interop.ExecuteJavaScript(@"alert(myFunction(2,3))");
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
// working as expected :
CSHTML5.Interop.ExecuteJavaScript(@"alert(myFunctionFromExternalJSfile(2,3))");
}
}
}
someFunctions.js javascript file:
function myFunctionFromExternalJSfile(a, b) {
return a * b;
}
Question: please, confirm / clarify is it a bug or expected behaviour by CSHTML5 design ?
Waiting for your kind reply,
Br,
Péter