2017-05-29 8 views
0

I持つの線に沿った内容とHTMLファイルレンダリングする電子アプリ:電子は、window.open()から適切な値を返さない

<html> 
    <head> 
    <script> 
     var myfunc = function() { 
     var data = "Some stuff to display in another window."; 
     var secondWindow = window.open(); 
     secondWindow.document.write(data); 
     }; 
    </script> 
    </head> 
    <body> 
    <button onclick="myfunc();">Open Second Window</button> 
    </body> 
</html> 

さて、これは私の通常のブラウザで動作する(Firefoxの)電子アプリではJSON.stringify(secondWindow);の場合は{'closed': false}となりますが、実際のウィンドウハンドルではありません(空のウィンドウは開いていますが)。

誰でもこの動作と、おそらくElectronで希望の結果を達成する方法について特別な光を当てることができますか?

電子アプリ:

const {app, BrowserWindow} = require("electron"); 
const path = require("path"); 
const url = require("url"); 

let win 

function createWindow() { 
    win = new BrowserWindow({width: 800, height: 600}) 
    win.loadURL(url.format({ 
    pathname: path.join(__dirname, 'index.html'), 
    protocol: 'file:', 
    slashes: true 
    })) 
    win.webContents.openDevTools({"detach": true}) 
    win.on('closed',() => { 
    win = null 
    }) 
} 

app.on('ready', createWindow) 

app.on('window-all-closed',() => { 
    if (process.platform !== 'darwin') { 
    app.quit() 
    } 
}) 

app.on('activate',() => { 
    if (win === null) { 
    createWindow() 
    } 
}) 

答えて

1

あなたはBrowserWindowを作成し、loadURL方法

// In the main process. 
const {BrowserWindow} = require('electron') 

// Or use `remote` from the renderer process. 
// const {BrowserWindow} = require('electron').remote 

let win = new BrowserWindow({width: 800, height: 600}) 
win.on('closed',() => { 
    win = null 
}) 

// Load a remote URL 
win.loadURL('https://github.com') 

// Or load a local HTML file 
win.loadURL(`file://${__dirname}/app/index.html`) 

UPDATE を呼び出す必要があり、メインコントローラ上のクリックイベントを処理してください:

var remote = require('electron').remote; 
$(document).on('click', '#btn', function() { 
    // Hide current window 
    var currWindow = remote.getCurrentWindow(); 
    currWindow.hide(); 
}); 

必ずメインウィンドウが

win.on('hide', function(e) { 
    showNewWindow(); 
}); 


var showNewWindow = function() { 
    newWindow = new BrowserWindow(windowOption); 
    newWindow .loadURL(`file://${__dirname}/app/new.html`) 
} 
+0

これはすでに私が主な電子アプリで持っているものですが、問題は読み込み中のHTMLファイルから2番目のウィンドウを開くことです。 (私は電子アプリのコンテンツを追加する質問を更新しました) – FatalKeystroke

+0

@FatalKeystroke私はちょうど私の答えを更新しました、それを確認してください。 –

0

を閉じている。これは、異なる方法で動作することを確認するために、メインウィンドウのhideイベントを処理します。メインプロセスで管理されているすべてのブラウザウィンドウインスタンス、メインプロセスで新しいウィンドウインスタンスを作成して現在のウィンドウの上に設定するか、現在のメインウィンドウでurlを再ロードする必要があります。この必要性がレンダラープロセスのロジックから発生する場合、特定のブラウザーウィンドウハンドルでメインプロセスで何らかのアクションを実行するために、メインプロセス(ipcRenderer.sendを介して)に通知する必要があります。現在のコードでは、ただ1つのブラウザウィンドウを作成します。レンダラープロセスでwindow.openを呼び出すと、この新しいインスタンスがメインプロセスから切り離され、制御できなくなります。

関連する問題