Access variable of render process from preload.js


#1

I have an object in my render process.
I need to access that object from preload.js

Expected o/p :

_render.js_

    var data = {
    "name" : "jk",
    "no":"9988"
    }

_preload.js_

console.log(data)

should give
     
{
        "name" : "jk",
        "no":"9988"
        }

I tried this example

render:

// In the render process.
global.sharedObject = {
  someProperty: 'default value'
}

preload:
console.log(require('electron').remote.getGlobal('sharedObject').someProperty)

I am getting error.
Is there any sample to access render process variable in preload.js


#2

preload String (optional) - Specifies a script that will be loaded before other scripts run in the page.
as stated in the doc, this script will run before ANY script in render process.

If the data won’t be used immediately, you could define a function accept that data as param in the preload script, and call the function when data is ready in render script.

If you need it synchronously, maybe you can try fs.read & eval.


#3

Thanks for suggestion but i cant make it work, for example if i create a function in

preload.js

function get_variable(data){
console.log(data)
}

Now how do i execute that function from my render with that ‘data param’


#4

preload.js

global.get_variable = function (data){
	console.log(data)
}

render.js

get_variable({'hkuc':'lion'});

register the function to global object will help you