You can use the following HTML and JavaScript to create a simple Insert Text from File object. The objectTag() function calls the readContentsOfFile() C function, which is stored in a library named myLibrary.

 <HTML> 
<HEAD> 
<SCRIPT> 
function objectTag() { 
    fileName = document.forms[0].myFile.value; 
    return myLibrary.readContentsOfFile(fileName); 
} 
</SCRIPT> 
</HEAD> 
 
<BODY> 
<FORM> 
Enter the name of the file to be inserted: 
<INPUT TYPE="file" NAME="myFile"> 
</FORM> 
</BODY> 
</HTML>

The readContentsOfFile() function accepts a list of arguments from the user, retrieves the filename argument, reads the contents of the file, and returns the contents of the file. For more information about the JavaScript data structures and functions that appear in the readContentsOfFile() function, see C-level extensibility and the JavaScript interpreter.

 JSBool 
readContentsOfFile(JSContext *cx, JSObject *obj, unsigned int ¬ 
argc, jsval *argv, jsval *rval) 
{ 
    char *fileName, *fileContents; 
    JSBool success; 
    unsigned int length; 
 
    /* Make sure caller passed in exactly one argument. If not, 
     * then tell the interpreter to abort script execution.    */ 
    if (argc != 1){ 
        JS_ReportError(cx, "Wrong number of arguments", 0); 
    return JS_FALSE; 
    } 
 
    /* Convert the argument to a string */ 
    fileName = JS_ValueToString(cx, argv[0], &length); 
    if (fileName == NULL){ 
        JS_ReportError(cx, "The argument must be a string", 0); 
        return JS_FALSE; 
    } 
 
    /* Use the string (the file name) to open and read a file */ 
    fileContents = exerciseLeftToTheReader(fileName); 
 
    /* Store file contents in rval, which is the return value passed 
    * back to the caller */ 
    success = JS_StringToValue(cx, fileContents, 0, *rval); 
    free(fileContents); 
 
    /* Return true to continue or false to abort the script */ 
    return success; 
}

To ensure that the readContentsOfFile() function executes properly and doesn’t cause a JavaScript error, you must register the function with the JavaScript interpreter by including a MM_Init() function in your library. When Dreamweaver loads the library at startup, it calls the MM_Init() function to get the following three pieces of information:

  • The JavaScript name of the function

  • A pointer to the function

  • The number of arguments that the function expects

The following example shows how the MM_Init() function for the library myLibrary might look:

 void 
MM_Init() 
{ 
    JS_DefineFunction("readContentsOfFile", readContentsOfFile, 1); 
}

Your library must include exactly one instance of the following macro:

 /* MM_STATE is a macro that expands to some definitions that are 
 * needed to interact with Dreamweaver. This macro must 
 * be defined exactly once in your library. */ 
MM_STATE

Note:

The library can be implemented in either C or C++, but the file that contains the MM_Init() function and the MM_STATE macro must be implemented in C. The C++ compiler garbles function names, which makes it impossible for Dreamweaver to find the MM_Init() function.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy