Javascript function won't work on Android


(Sergio) #1

I have created a Hype document where I call a Javascript function to change the inner html of a rectangle to create a UI toggle button based on the name of the img src filename as well as incrementing a counter I use elsewhere.

Everything works fine on desktop browsers and even when I create a webView based iOS application in Xcode

Android, for some reason throws an error. Here is the code:

var whichone = document.getElementById('depc_btn');
var check = whichone.src.includes("pressed");

	if (check == true) 
	{
		whichone.src = whichone.src.replace("depcheck_button_pressed.svg", "depcheck_button.svg");
		if (count >= 1) {count --;}
	}
else if (check == false)
	{
		whichone.src = whichone.src.replace("depcheck_button.svg", "depcheck_button_pressed.svg");
		count ++;
	}

document.getElementById('count_txt').innerHTML = count;

The error is thrown before the ‘check’ variable is created. Any ideas?


(Daniel) #2

Checking the code with JSHint, it gives out two warnings:

15	Use '===' to compare with 'true'.
20	Use '===' to compare with 'false'.

Also, count is not defined as a variable.

Not sure if that’s already the solution, since I’m no JS expert.


(Sergio) #3

Thanks for the reply.

‘count’ is declared in the Head HTML code and during a specific scene load as i want to ensure it is always reset to 0 at certain times. Using ‘===’ instead of ‘==’ still resulted in the same error.


#4

String.prototype.includes() is not supported in Android and pretty much any mobile platform (exception Firefox (Gecko) 40 and maybe Safari since you got it to work in a webview)

Maybe use indexOf which is supported throughout all.


(Sergio) #5

That did the trick! Thanks!