Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package net.sourceforge.jsdialect.util;
 
 
 
 // FIXME: unit test this
public class JavaScriptComposer {
    public static final String DOCUMENT_READY_BLOCK_ID = "onDocumentReadyBlockId";
    public static final String WINDOW_LOAD_BLOCK_ID = "onWindowLoadBlockId";
    private JavaScriptComposer() {
    }

    
Adds some code to jQuery "on document ready" block. If the block does not exist previously, create it.
    public static void addOnDocumentReady(Document documentString code) {
        String eventDeclaration = "$(document).ready";
        addOnDocumentReady(documentcodeeventDeclaration);
    }
    
Adds some code to jQuery "on document ready" block. If the block does not exist previously, create it.
    public static void addOnWindowLoad(Document documentString code) {
        String eventDeclaration = "$(window).load";
        addOnDocumentReady(documentcodeeventDeclaration);
    }
    private static void addOnDocumentReady(Document documentString codeString blockIdString eventDeclaration) {
        // Create or locate script
        Element script = DomUtils.getElementById(document"script"blockId);
        if (script == null) {
            script = new Element("script");
            script.setAttribute("type""text/javascript");
            script.setAttribute("id"blockId);
            String text = "\n//<![CDATA[\n" + eventDeclaration + "(function(){\n});\n//]]>\n";
            script.addChild(new Text(textfalse));
            DomUtils.getElementsByTagName(document"body").get(0).addChild(script);
        }
        // Build indented code
        StringBuilder codeSb = new StringBuilder();
        StringTokenizer lines = new StringTokenizer(code"\n");
        while (lines.hasMoreTokens()) {
            codeSb.append("\t").append(lines.nextToken()).append("\n");
        }
        codeSb.append("");
        // Add new code to existent script text
        StringBuilder scriptSb = new StringBuilder(((Textscript.getFirstChild()).getContent());
        scriptSb.delete(scriptSb.length() - "});\n//]]>\n".length(), scriptSb.length());
        scriptSb.append(codeSb.toString());
        scriptSb.append("});\n//]]>\n");
        ((Textscript.getFirstChild()).setContent(scriptSb.toString());
    }
New to GrepCode? Check out our FAQ X