Node -webkit How to call a function in another window?

I created a new window with this piece of code and tried to send some data, but the function is not called, maybe I'm doing it wrong?

script.js on index.html

var path = require('path');
element.onclick = function(){
    var win = gui.Window.get(window.open('listdir.html'));
    var location = path.resolve(process.cwd(),'fontsFolder');
    win.eval(null, 'listdir("'+location+'")'); //Is there a node function to parse '\'?
};

listdir.js on listdir.html

function listdir(directory){
    alert("directory "+directory); //never called
}

Error:

ReferenceError: listdir is not defined
    at <anonymous>:1:1
    at Window.init.Window.eval (window_bindings.js:486:16)
    at HTMLLIElement.element.onclick (file:///C:/../AppData/Local/Temp/nw3528_1882/js/script.js:29:12)
+4
source share
1 answer

Well, this may not be the correct answer to the question "How to call a function in another window," but the answer to your initial problem is "How to send parameters to a new window" (before editing the name).

HTML5, sessionStorage ( , ).

:

index.html( )

<!DOCTYPE html>
<html>
<body>
    Test <a href="">Click</a>

    <script src="jquery-1.11.1.min.js"></script>
    <script>
        var mySharedObj = {
            'one': 1,
            'two': 2,
            'three': 3
        };

        // node-webkit specific
        var gui = require('nw.gui');

        $('a').click(function() {
            var win = gui.Window.get(window.open('index2.html'));
            win.eval(null, 'sessionStorage.setItem(\'mySharedJSON\', \''+JSON.stringify(mySharedObj)+'\');');
        });
    </script>
</body>

index2.html( , window.open:

<!DOCTYPE html>
<html>
<body>
    Test 2: 

    <script src="jquery-1.11.1.min.js"></script> 
    <script>
        $(document).ready(function() {
            // Process sharedObj when DOM is loaded
            var mySharedObj = JSON.parse(sessionStorage.getItem('mySharedJSON'));

            // Now you can do anything with mySharedObj

            console.log(mySharedObj);
        });
    </script>
</body>

, ? window.eval (. ) , . , , script , DOM , JavaScript . , ( window). , , JSON window.sessionStorage. , .

: , .

+1

Source: https://habr.com/ru/post/1547637/


All Articles