Running Javascript

Discussion and feedback on Construct 2

Post » Fri Nov 28, 2014 6:52 pm

I'am trying to use the action ExecuteJavaScript in the Browser object. I placed two objects in my stage, a TextBox with a ID = "myid" and a button. When I click the button its use ExecuteJavaScript method with this:
Code: Select all
"myFunction()
{
   document.elementById("myid").innerHTML = "TESTE";
}"

What I need to do to get this type of code working?
Thanks!
Be nice with others and with yourself!
My musics: https://soundcloud.com/vinians
My C2 Forum:
http://nmbr.forumeiros.com/
My Site:
http://www.dicasebytes.com.br
B
22
S
5
G
6
Posts: 167
Reputation: 4,949

Post » Fri Nov 28, 2014 10:24 pm

Is there a reason for using js to do this?
Just curious because you could do this in events

Code: Select all
Button on click
   textbox set text "..."


Otherwise here's how you'd do it with js

Code: Select all
Button on click
   Exec js "document.getElementById('myid').value = '...';"


// or
Code: Select all
Button on click
   Exec js "myid.value= '...';"
B
11
S
2
G
1
Posts: 202
Reputation: 1,580

Post » Fri Nov 28, 2014 10:34 pm

My understanding is running javascript directly is very risky, especially if you are handling any user input. See here:

https://www.scirra.com/manual/110/browser
Don't lose your work. Backup your game with Dropbox.
B
44
S
10
G
10
Posts: 1,106
Reputation: 9,202

Post » Fri Nov 28, 2014 11:22 pm

1. Your using the same open and closing " " in one string line.
2. You declaring a function. I suggest you don't and just use the line by itesle. unless your function becomes really largly functional

No
"myFunction()
{
document.elementById("myid").innerHTML = "TESTE";
}"


Try
" document.elementById('myid').innerHTML = 'test'; "

notice the change in the qoute type for internal qoutes.
B
90
S
18
G
9
Posts: 2,455
Reputation: 15,018

Post » Sat Nov 29, 2014 12:38 am

jayderyu wrote:2. You declaring a function. I suggest you don't and just use the line by itesle. unless your function becomes really largly functional

One reason I tend to use a function is so I can call it with the window object.
Code: Select all
"// 'this' is a refference to the browser plugin
console.log(this);

// 'this' is  the window object
function func(){console.log(this);}
func.call(window);
"
B
11
S
2
G
1
Posts: 202
Reputation: 1,580

Post » Sat Nov 29, 2014 5:21 am

Then you need to store the function in a long term library. As it is now it's stored in a temporary closure that will be gone.

"window.mylib.func = function(){ console.log( 'nested text string' ); "

then else where
"window.mylib.func()"

but do you really need to put it in a function. if you start needing these functions that's usually the time to move to the SDK and embed the functionality. Keep in mind that eval is never put into the JIT. So eval will always run at the slowest code execution. So eval is only good for the occasional js functionality.
B
90
S
18
G
9
Posts: 2,455
Reputation: 15,018

Post » Sat Nov 29, 2014 12:05 pm

Thank you guys for your reply!
About the meaning I need it is because I need to put some CSS in action. Moreover you get more control about all game elements. I would like to make some plugins but I was reading the docs and I found it very difficult at first glance, I will try to read more times and trying to understand it. Ah and I got to get is working now.
[]s
Be nice with others and with yourself!
My musics: https://soundcloud.com/vinians
My C2 Forum:
http://nmbr.forumeiros.com/
My Site:
http://www.dicasebytes.com.br
B
22
S
5
G
6
Posts: 167
Reputation: 4,949


Return to Construct 2 General

Who is online

Users browsing this forum: Laura_D and 9 guests