How to navigate to different pages in a chrome app without creating a new window?

狂风中的少年 提交于 2019-12-11 22:32:12

问题


My Chrome app contains two pages A1.html and A2.html. How can i navigate from A1.html to A2.html without creating a new window?

The page A1.html contains 10 div's and A2.html contains a back link to A1.html. My need is to load directly a specific div of A1.html when the back link in A2.html is pressed.


回答1:


You can't navigate within a Chrome App window (Content Security Policy), although you can navigate to an external browser if you set the target attribute of the <a> element to "_blank". What you have to do, if you really want links to appear to work normally, is intercept the click on the link by setting an event handler and then changing the DOM from within JavaScript.

One easy way to change the DOM from JavaScript if you have an HTML fragment is to use the insertAdjacentHTML API (Google it for documentation).

While this might seem awkward, even limiting, think of a Chrome App as an app, and not a web page. After all, with a native Mac OS X or Windows app, you wouldn't expect to entirely change the UI in a window by simply clicking on a button, right? You'd expect that the app would do that via the native API. Same the Chrome Apps.

Alternatively, you can position a webview in the Chrome App window, and then HTML within the webview works normally, because that really is a "web view."



来源:https://stackoverflow.com/questions/26348790/how-to-navigate-to-different-pages-in-a-chrome-app-without-creating-a-new-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!