add code in index.html

Get help using Construct 2

Post » Sun Nov 20, 2011 3:16 pm

Is it possible to add some code directly in the exported index.html via an existing plugin ? If not, is it possible to do this directly with doing a plugin ?

B
44
S
12
G
13
Posts: 628
Reputation: 10,471

Post » Sun Nov 20, 2011 4:36 pm

What do you need to add? It may be possible depending on what you want.
Scirra Founder
B
359
S
214
G
72
Posts: 22,949
Reputation: 178,554

Post » Sun Nov 20, 2011 5:01 pm

I would like call an API as
<script type="text/javascript" src="http://***API_URL***"></script>
B
44
S
12
G
13
Posts: 628
Reputation: 10,471

Post » Sun Nov 20, 2011 5:09 pm

Most APIs have an asynchronous option which is better since it improves loading times - you can dynamically create a script in your plugin's startup code and usually it then makes a callback to say it's loaded. That's what the Facebook plugin does, it loads Facebook's script dynamically without adding any script tag to the HTML page.

However if you're stuck with an API that doesn't support that, there's an undocumented feature that lets you do that - add a dependency property to GetPluginSettings() in edittime.js like this:

[code]"dependency": "myfile.js"[/code]

This will then add a script tag for "myfile.js" in the HTML and copy myfile.js from your plugin's folder to the exported directory. However, I would reiterate that using APIs asynchronously is better. Which API is it, out of curiosity?
Scirra Founder
B
359
S
214
G
72
Posts: 22,949
Reputation: 178,554

Post » Sun Nov 20, 2011 5:18 pm

Tutorial on the subject. Why try to make plugins ?
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,999
Reputation: 57,793

Post » Sun Nov 20, 2011 5:36 pm

@Kyatric, I think they were asking about automatically modifying it from a plugin rather than by hand.
Scirra Founder
B
359
S
214
G
72
Posts: 22,949
Reputation: 178,554

Post » Sun Nov 20, 2011 10:43 pm

Ashley, i'ts Google Maps API.
Thanks for your help.
B
44
S
12
G
13
Posts: 628
Reputation: 10,471

Post » Mon Nov 21, 2011 9:19 pm

Hey,
I'm sincerely sorry to behave as a noob...
That's true, I'm not a javascript coder but I really begin to understand it as well as the C2's SDK.
Enough to do 5-6 functions in my "workinprogress" plugin in a short time of learning.

I looked through the code of the facebook plugin. Read about asynchronous option for the Google Maps API.

Nevertheless, I didn't figure out.

Let me just explain you some things.

I put this in a "googleapi.js" file
function loadScript()
{
   var script = document.createElement("script");
   script.type = "text/javascript";
   script.src = "http://maps.googleapis.com/maps/api/js?sensor=true";
   document.head.appendChild(script);
}
   window.onload = loadScript;


I "dependency" it in my edittime.js
It didn't work. I tried to put this function directly into instanceProto.onCreate = function() as well. But it didn't work too.
Each time, I can see the link correctly added in the <head> of my html test in FireBug.
But google is always not defined.

The API is correctly called only when I put the link manually into the index.html after I exported it.

Thanks for your help. I'm really grateful.
B
44
S
12
G
13
Posts: 628
Reputation: 10,471


Return to How do I....?

Who is online

Users browsing this forum: HeinRich and 23 guests