Dilvish5
Dilvish5

Reputation: 310

node-webkit running a function from external source

I am loading a remote page with an iframe in node-webkit app. I would like to run a function from the node.js app from the webpage.

In app.js i have

var xxx = function(){ console.log('test'); }

and in http://www.test.com/index.html i have tried:

window.xxx();
xxx();
global.xxx();

But nothing seems to work.

How can i do that?

Many thanks

Upvotes: 0

Views: 138

Answers (1)

OldGeeksGuide
OldGeeksGuide

Reputation: 2918

If I understand correctly, you want to access a function that's defined in the remote page that you are including as an iframe in your current page, right?

There are two separate issue here, I think.

First, you need to have a handle on the iframe, then access that window's environment through the 'contentWindow' property of the iframe dom element, i.e. given an iframe with id foo, that points to a page with a function named bar, you could do this:

x = document.getElementById('foo');
x.bar();

This works fine for me with a local page, but the other issue is that you might find some difficulty with running a remote page in an iframe and still having access. If it's a page under your control, that might work, but some pages don't like to be run in iframes, so then you have to sandbox to some extent, which may interfere with your ability to access it in this way. It's been a while since I played with sandboxing, so I'm not sure, but if it's a page you control, you should be able to set it up so it's not a problem.

Upvotes: 1

Related Questions