最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - xul: open a local html file relative to "myapp.xul" in xul browser - Stack Overflow

programmeradmin2浏览0评论

in short: is there any way to find the current directory full path of a xul application?

long explanation:

I would like to open some html files in a xul browser application. The path to the html files should be set programmatically from the xul application. The html files reside outside the folder of my xul application, but at the same level. (users will checkout both folders from SVN, no installation available for the xul app)

It opened the files just fine if I set a full path like "file:///c:\temp\processing-sample\index.html"

what i want to do is to open the file relative to my xul application.

I found i can open the user's profile path:

var DIR_SERVICE = new Components.Constructor("@mozilla/file/directory_service;1", "nsIProperties");
var path = (new DIR_SERVICE()).get("UChrm", Components.interfaces.nsIFile).path;
var appletPath;

// find directory separator type
if (path.search(/\\/) != -1)
{
appletPath = path + "\\myApp\\content\\applet.html"
}
else
{
appletPath = path + "/myApp/content/applet.html"
}

// Cargar el applet en el iframe
var appletFile = Components.classes["@mozilla/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
appletFile.initWithPath(appletPath);
var appletURL = Components.classes["@mozilla/network/protocol;1?name=file"].createInstance(Components.interfaces.nsIFileProtocolHandler).getURLSpecFromFile(appletFile);
var appletFrame = document.getElementById("appletFrame");
appletFrame.setAttribute("src", appletURL); 

is there any way to find the current directory full path of a xul application?

in short: is there any way to find the current directory full path of a xul application?

long explanation:

I would like to open some html files in a xul browser application. The path to the html files should be set programmatically from the xul application. The html files reside outside the folder of my xul application, but at the same level. (users will checkout both folders from SVN, no installation available for the xul app)

It opened the files just fine if I set a full path like "file:///c:\temp\processing-sample\index.html"

what i want to do is to open the file relative to my xul application.

I found i can open the user's profile path:

var DIR_SERVICE = new Components.Constructor("@mozilla/file/directory_service;1", "nsIProperties");
var path = (new DIR_SERVICE()).get("UChrm", Components.interfaces.nsIFile).path;
var appletPath;

// find directory separator type
if (path.search(/\\/) != -1)
{
appletPath = path + "\\myApp\\content\\applet.html"
}
else
{
appletPath = path + "/myApp/content/applet.html"
}

// Cargar el applet en el iframe
var appletFile = Components.classes["@mozilla/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
appletFile.initWithPath(appletPath);
var appletURL = Components.classes["@mozilla/network/protocol;1?name=file"].createInstance(Components.interfaces.nsIFileProtocolHandler).getURLSpecFromFile(appletFile);
var appletFrame = document.getElementById("appletFrame");
appletFrame.setAttribute("src", appletURL); 

is there any way to find the current directory full path of a xul application?

Share Improve this question edited Sep 30, 2008 at 14:54 LLucasAlday asked Sep 19, 2008 at 18:20 LLucasAldayLLucasAlday 2,57111 gold badges36 silver badges41 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

I found a workaround: http://developer.mozilla/en/Code_snippets/File_I%2F%2FO i cannot exactly open a file using a relative path "../../index.html" but i can get the app directory and work with that.

var DIR_SERVICE = new Components.Constructor("@mozilla/file/directory_service;1", "nsIProperties");
var path = (new DIR_SERVICE()).get(resource:app, Components.interfaces.nsIFile).path;
var appletPath;

Yes, html files within your extension can be addressed using chrome URIs. An example from one of my extensions:

content.document.location.href = "chrome://{appname}/content/logManager/index.html"

In a xul application, you can access the chrome folder of the application using a chrome url.

I have relatively little experience with the element , so I'm not sure if this will work exactly for you. It is the way you include javascript source files in your xul file:

<script src="chrome://includes/content/XSLTemplate.js" type="application/x-javascript"/>

So, perhaps if the file resides at c:\applicationFolder\chrome\content\index.html , you can access it at:

chrome://content/index.html 

in some fashion.

You may also look at jslib, a library that simplifies a lot of things, including file i/o. IIRC, I had trouble getting to a relative path using '../', though.

发布评论

评论列表(0)

  1. 暂无评论