Running Javascript

  • 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:

    "myFunction() 
    {
       document.elementById("myid").innerHTML = "TESTE";
    }"
    [/code:3n5n0jyi]
    What I need to do to get this type of code working?
    Thanks!
  • Is there a reason for using js to do this?

    Just curious because you could do this in events

    Button on click
    	textbox set text "..."[/code:2pia64c5]
    
    Otherwise here's how you'd do it with js
    
    [code:2pia64c5]Button on click
    	Exec js "document.getElementById('myid').value = '...';"[/code:2pia64c5]
    
    //	or
    [code:2pia64c5]Button on click
    	Exec js "myid.value= '...';"[/code:2pia64c5]
  • 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

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

    "// 'this' is a refference to the browser plugin
    console.log(this); 
    
    // 'this' is  the window object
    function func(){console.log(this);}
    func.call(window);
    "
    [/code:15t6a7kw]
  • 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.

  • 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

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)