Wednesday, December 23, 2009

Using different JQuery versions in the same page

Suppose your web page in the CMS is already using one version of jquery and you want to add a new HTML element with a javascript library that needs a more recent version of jquery.

You may think to use the google api like:

<script>
google.load("jquery", "1.3.2");
</script>



and then use the noConflict option like:




<script>
function OnLoad(){
jq13 = jQuery.noConflict(true);
}

google.load("jquery", "1.3.2");
google.setOnLoadCallback(OnLoad);
</script>



Unfortunately this will not work correctly in IE…so you may want to use the technique to dynamically load a javascript (see http://themnd.blogspot.com/2009/12/dynamically-load-javascript-on-webpage.html)






<script>
function OnLoad(){
jq13 = jQuery.noConflict(true);
}

function loadJQuery() {
loadJS('http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', function() {
OnLoad();
});
}

jQuery(document).ready(function() {
setTimeout(function() {
loadJQuery();
}, 2000);
});
</script>



at this point you can load the needed library in the OnLoad function before setting the noConflict option. Please note that we are still using the google cdn to fetch the jquery library. The OnLoad function would be something like:




function OnLoad(){
loadJS(plugin_url, function() {
jq13 = jQuery.noConflict(true);
// now you can call the plugin methods here:
jq13.XXXX()
});
}

Dynamically load javascript on a webpage

When you work with a CMS sometimes you may need to dynamically add a javascript library on the fly.

The easiest thing to do is:

var headID = document.getElementsByTagName("head")[0];         
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = url;
headID.appendChild(newScript);



But what if you want to be notified when the javascript has been loaded? You can add the onload parameter:




newScript.onload = function(){
func();
};



but you will find out that this will not work in IE, so what you really need to use is:




function loadJS(url, func) {
var headID = document.getElementsByTagName("head")[0];
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
if (newScript.readyState) { //IE
newScript.onreadystatechange = function() {
if (newScript.readyState == "loaded" || newScript.readyState == "complete") {
newScript.onreadystatechange = null;
func();
}
};
} else { //Others
newScript.onload = function(){
func();
};
}
newScript.src = url;
headID.appendChild(newScript);
}



We will rely on the onreadystatechange event that IE will fire, unfortunately that event is, in reality, the progress of the loading and sometimes it reach the “complete” state, sometimes only the “loaded” state, so if we really want to be sure that our callback will be called we have to get the first one and then cancel the event.



You can learn more at: http://www.nczonline.net/blog/2009/06/23/loading-javascript-without-blocking/



In a similar way you can also dynamically load CSS:




function loadCss(url) {
var headID = document.getElementsByTagName("head")[0];
var cssNode = document.createElement('link');
cssNode.type = 'text/css';
cssNode.rel = 'stylesheet';
cssNode.href = url;
cssNode.media = 'screen';
headID.appendChild(cssNode);
}

Saturday, October 03, 2009

Tuesday, August 18, 2009

Creating an HTML layout that is cross browser x Dummies

This post is mostly for who have to use an HTML template generated by an external Web designer and making it “dynamic”, that means we are handed the html page and we have to create the template that will be used by a web application to render content using that templates like polopoly (www.atex.com) but it is also true for other systems like joomla.

1. do not use cellpadding and cellspacing in tables, set them to 0 and eventually use CSS to control spacing, firefox, chrome and internet explorer interprets them differently.

2. do not use SPACER tag since internet explorer will not understood it, use a transparent 1 pixel gif with an img tag instead.

3. do not comment a css line with // since internet explorer will read it anyway, comment using /* and */ if you really need to.

4. if you start doing evaluate the layout with internet explore it would be more easy to adjust it for firefox and chrome (if any adjustment needs to be made).

5. always start with IE6 if you need to target it since it is the poorer browser that probably will need more care.

Wednesday, August 05, 2009

How to execute a command line tool from a servlet

In a java program when you want to execute an external program you will normally write something like this:

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
proc.waitFor();


Where cmd is the command line needed to be executed.



However, if you run the same piece of code from a tomcat servlet it may just hung up until you kill it. The problem is documented in the documentation of the exec method, you need to consume the inputStream and the outputStream, so a quick way of doing it is to start other two threads that will consume those stream.



First create the thread class that will process the stream:



class StreamGobbler extends Thread
{
InputStream is;
String type;

StreamGobbler(InputStream is, String type)
{
this.is = is;
this.type = type;
}

public void run()
{
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null) {
System.out.println(type + ">" + line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}



Now we can modify the previous code to handle InputStream and ErrorStream:



Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
StreamGobbler errorGlobber = new StreamGobbler(proc.getErrorStream(), "CMDTOOL-E");
StreamGobbler outputGlobber = new StreamGobbler(proc.getInputStream(), "CMDTOOL-O");
errorGlobber.start();
outputGlobber.start();
proc.waitFor();


et voilĂ  the code will execute just fine in a servlet.



The idea and code has been taken from: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1

Thursday, July 23, 2009

Apache mod_proxy and tomcat

If you use apache to forward request to a local running tomcat, you may encounter this error message in the error.log:

[error] (13)Permission denied: proxy: HTTP: attempt to connect to 127.0.0.1:8080 (*) failed


If you have SELinux running you have to check if it allows local connections, by running:



getsebool -a | grep httpd


you have to check that the following is written:



httpd_can_network_connect –> on


if it is off, you have to enable it:



setsebool -P httpd_can_network_connect=1


and restart your apache.



This tip has been taken from: http://swapyourbooks.com/welcome/knowledgebase/misc/selinux-allow-httpd-tomcat-proxy.html

Friday, June 13, 2008

C++ member template function

With Visual Studio 2003 you can use member template functions, for example:

class Foo {

    template <class T> void bar(T* t) {}

};

that you can call in this way:

void main() {
  int a = 10;
  Foo obj;
  obj.bar(a);

}

Make sure that when you declare the bar method you include also the method definition otherwise you will end-up with a linker error.

Saturday, May 31, 2008

Sunday, September 16, 2007

Python Soap Server and ASP.Net Web Services - Part 1

I had a soap server built in python with SOAPpy and I wanted to use it from an ASP.Net application using the visual studio 2005 built in web services tool. I encountered several problems:

  • Visual Studio wants to read the WSDL from the soap server;
  • Namespaces matter.

For the WSDL problem you have just to define a function like:

def wsdl(self):
  wsdl_text = ''
  f = open('service.wsdl', 'r')
  for l in f.readlines():
    wsdl_text += l f.close()
  return wsdl_text

for the namespece part, suppose you ns is 'http://localhost/MyServer/2007/09/Services/MyService/01', you server will be:

class MySoapServer(SOAPServer):
  def __init__(self, ip = "localhost", port = 8080):
  from SOAPpy import NS

  # visual studio want's the soap envelope defined as 's'
  NS.ENV_T = "s"
  self.ns = 'http://localhost/MyServer/2007/09/Services/MyService/01'
  SOAPServer.__init__(self, (ip, port))
  self.registerFunction('wsdl')
  self.registerFunction('soap_myFunction', 'myFunction', namespace = self.ns)

In this way Visual Studio will be able to correctly call your exposed methods. Later we will see how to let Visual Studio correctly understand your results.

Technorati Tags: , ,

Monday, January 10, 2005

Adobe SDK - K2Vector

I had problem with a copy of a K2Vector, basically I had
an interface's method like:


class fooInterface {

  virtual void setVector(const K2Vector<fooObject>& src) { fVec =
src; };

protected:

  K2Vector<fooObject> fVec;

}


The linker's error I had was like "no operator= was
defined for K2Vector<fooObject>"...I had the K2Vector.h included but it
didn't solved my problem. It was not the first copy between vectors in the
plugin so I was a bit shooked by this error, after tries I include
"K2Vector.tpp" and that solved the problem. Really strange.
If you look at
the bottom of K2Vector.h you can see that it include "K2Vector.tpp".


Anyway, that's solved my problem and another lesson learned.


 

Thursday, December 16, 2004

Adobe SDK - plugin conflict

I had a problem with an InDesign plugin I was developing lately, the problem occours when you run InDesign and InDesign tell you that the plugin will conflicts with itself.
After a lot of tries (the plugin was pretty empty) I found out that the problem was a command that was something like:

Class
{
kSampleCmdBoss,
kInvalidClass,
{
IID_ICOMMAND, kSampleCmdImpl,
IID_ISTRINGDATA, kStringDataImpl,
IID_ISAMPLENAMEDATA, kStringDataImpl,
}
},

The culprit is the IID_ISAMPLENAMEDATA, it seems like a commands cannot have two standard kStringDataImpl, so the solution is to replace the second kStringDataImpl, to do this we need to simply define an implementation alias like this:


resource ImplementationAlias(1)
{
{
kSampleNameDataImpl, kStringDataImpl,
}
};


so now we can modify our command in this way:

Class
{
kSampleCmdBoss,
kInvalidClass,
{
IID_ICOMMAND, kSampleCmdImpl,
IID_ISTRINGDATA, kStringDataImpl,
IID_ISAMPLENAMEDATA, kSampleNameDataImpl,
}
},


and don't forget to add the kSampleNameDataImpl implementation if the xxxID.h


Friday, November 19, 2004

CControlView

If you are compiling a custom widget that derives from CControlView and the linker fails saying that it cannot find some methods of CControlView, it is high probable that you have remove NO_STRICT from the project preprocessor options.

Wednesday, September 15, 2004

How to import an Image with InCopy

Just look at the InDesign knowledge base (http://support.adobe.com/devsup/devsup.nsf/docs/50352.htm).

Hint: use the stream Luke.

Monday, August 02, 2004


Last night I didn't sleep to much...here it is a photo of the moon shooted at 4am. Posted by Hello

Monday, July 26, 2004


Me anche Chiara having fun in Stresa Posted by Hello

Wednesday, July 07, 2004

Indesign SDK

I was trying to understand how to catch a menu action for a standard Indesign menu item. Looking trough the indesign forum I found out a couple of messages that points to the knowledge base, in particular to "How To: Intercept the File>Open menu event" and "How To: Get the filename from File>New or File>Open menu dialogs".

Adobe Indesign web pages

There are some really useful pages in the adobe web site regarding incopy/indesign development:

Indesign Developer Knowledgebase

Indesign user to user forum

 

Tuesday, July 06, 2004

Food

I found out this site that contains a lot of useful information for who cooks rarely (like me).

Friday, July 02, 2004

Indesign SDK - plugin ID

Don't forget to register you plugin with Adobe in order to have an unique Plugin ID. Adobe provided an How to explaining all the necessary steps. The registration page is here.

Thursday, July 01, 2004

Indesign SDK - Front Document

When you want to obtain a pointer to the current front document (the one the user is editing) you could use

InterfacePtr doc(Utils()->GetFrontDocument(), UseDefaultIID());
but when you are inside an action component it may not work, instead you should use
InterfacePtr doc(ac->GetContextDocument(), UseDefaultIID());
where ac is the IActiveContext* passed in the DoAction.