Re: Inserting contents in <head>
Re: Inserting contents in <head>
- Subject: Re: Inserting contents in <head>
- From: Jean-François Veillette <email@hidden>
- Date: Mon, 06 Mar 2006 08:29:43 -0500
We have generalized this in our generic WOComponent super-class.
GenericWOComponent.java ...
public void appendToResponse(WOResponse res, WOContext ctx) {
super.appendToResponse(res, ctx);
addRequiredWebResources(res);
}
public void addJSResource(WOResponse res, String fileName) {
NSMutableDictionary uInfo = responseUserInfo();
if(uInfo.objectForKey(fileName) == null) {
uInfo.setObjectForKey(fileName, fileName);
WOResourceManager worm = WOApplication.application().resourceManager();
String url = worm.urlForResourceNamed(fileName, null, session().languages(), context().request());
String js = "<script type=\"text/javascript\" src=\""+ url +"\"></script>";
insertInResponseBeforeTag(res, js, htmlCloseHead());
}
}
public void insertInResponseBeforeTag(WOResponse res, String content, String tag) {
String stream = res.contentString();
int idx = stream.indexOf(tag);
String pre = stream.substring(0,idx);
String post = stream.substring(idx, stream.length());
res.setContent(pre+content+post);
}
public NSMutableDictionary responseUserInfo() {
NSDictionary d = context().response().userInfo();
NSMutableDictionary r = null;
if(d == null) {
r = new NSMutableDictionary();
context().response().setUserInfo(r);
} else {
if(d instanceof NSMutableDictionary) {
r = (NSMutableDictionary)d;
} else {
r = d.mutableClone();
context().response().setUserInfo(r);
}
}
return r;
}
public String htmlCloseHead() {
// our real code use something like System.getProperties to get this value
return "</head>";
}
Then we use it in our specialized components.
SpecializedWOComponent.java ...
public void addRequiredWebResources(WOResponse res) {
// add a javascript 'include' in the 'header'
addJSResource(res, "SpecializedWOComponent.js");
}
You get the idea, you can use this technique to add css include, add custom initialization, etc.
In a project, where we have a common 'frame' where the '<body>' tag is well defined, we can even add script to the body.onLoad custom function using a variation of this technique.
Look at the code in the DemoAjax project, it is using this technique for the JSProxyRPC.
I first feel like it wasn't the right way to do, and I tried to deal with the DOM tree instead. But as the html is incomplete (when you get it in appentToResponse in a sub-component), the parser wasn't able to parse the response content and give a DOM tree of it. So this wasn't an option.
This is the best I found, but I still think a DOM like approach would be better.
- jfv
Le 06-03-06, à 05:56, Vincent Coetzee a écrit :
Dear List
I have a WebObjects component that I have developed that is a partial document. It preferably needs to insert some content into the <head></head> portion of a page, and additionally only needs to insert it if it is no already there. How does one insert content into <head></head> and test whether it has already been inserted.
Any pointers would be appreciated.
Vincent
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden