| 1 |
lars |
1 |
<html>
|
|
|
2 |
<head>
|
|
|
3 |
<title>Example of HTMLArea 3.0</title>
|
|
|
4 |
|
|
|
5 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
|
6 |
|
|
|
7 |
<!-- Configure the path to the editor. We make it relative now, so that the
|
|
|
8 |
example ZIP file will work anywhere, but please NOTE THAT it's better to
|
|
|
9 |
have it an absolute path, such as '/htmlarea/'. -->
|
|
|
10 |
<script type="text/javascript">
|
|
|
11 |
_editor_url = "../";
|
|
|
12 |
_editor_lang = "en";
|
|
|
13 |
</script>
|
|
|
14 |
|
|
|
15 |
<!-- load the main HTMLArea file, this will take care of loading the CSS and
|
|
|
16 |
other required core scripts. -->
|
|
|
17 |
<script type="text/javascript" src="../htmlarea.js"></script>
|
|
|
18 |
|
|
|
19 |
<!-- load the plugins -->
|
|
|
20 |
<script type="text/javascript">
|
|
|
21 |
// WARNING: using this interface to load plugin
|
|
|
22 |
// will _NOT_ work if plugins do not have the language
|
|
|
23 |
// loaded by HTMLArea.
|
|
|
24 |
|
|
|
25 |
// In other words, this function generates SCRIPT tags
|
|
|
26 |
// that load the plugin and the language file, based on the
|
|
|
27 |
// global variable HTMLArea.I18N.lang (defined in the lang file,
|
|
|
28 |
// in our case "lang/en.js" loaded above).
|
|
|
29 |
|
|
|
30 |
// If this lang file is not found the plugin will fail to
|
|
|
31 |
// load correctly and nothing will work.
|
|
|
32 |
|
|
|
33 |
HTMLArea.loadPlugin("TableOperations");
|
|
|
34 |
HTMLArea.loadPlugin("SpellChecker");
|
|
|
35 |
HTMLArea.loadPlugin("FullPage");
|
|
|
36 |
HTMLArea.loadPlugin("CSS");
|
|
|
37 |
HTMLArea.loadPlugin("ContextMenu");
|
|
|
38 |
</script>
|
|
|
39 |
|
|
|
40 |
<style type="text/css">
|
|
|
41 |
html, body {
|
|
|
42 |
font-family: Verdana,sans-serif;
|
|
|
43 |
background-color: #fea;
|
|
|
44 |
color: #000;
|
|
|
45 |
}
|
|
|
46 |
a:link, a:visited { color: #00f; }
|
|
|
47 |
a:hover { color: #048; }
|
|
|
48 |
a:active { color: #f00; }
|
|
|
49 |
|
|
|
50 |
textarea { background-color: #fff; border: 1px solid 00f; }
|
|
|
51 |
</style>
|
|
|
52 |
|
|
|
53 |
<script type="text/javascript">
|
|
|
54 |
var editor = null;
|
|
|
55 |
function initEditor() {
|
|
|
56 |
|
|
|
57 |
// create an editor for the "ta" textbox
|
|
|
58 |
editor = new HTMLArea("ta");
|
|
|
59 |
|
|
|
60 |
// register the FullPage plugin
|
|
|
61 |
editor.registerPlugin(FullPage);
|
|
|
62 |
|
|
|
63 |
// register the SpellChecker plugin
|
|
|
64 |
editor.registerPlugin(TableOperations);
|
|
|
65 |
|
|
|
66 |
// register the SpellChecker plugin
|
|
|
67 |
editor.registerPlugin(SpellChecker);
|
|
|
68 |
|
|
|
69 |
// register the CSS plugin
|
|
|
70 |
editor.registerPlugin(CSS, {
|
|
|
71 |
combos : [
|
|
|
72 |
{ label: "Syntax:",
|
|
|
73 |
// menu text // CSS class
|
|
|
74 |
options: { "None" : "",
|
|
|
75 |
"Code" : "code",
|
|
|
76 |
"String" : "string",
|
|
|
77 |
"Comment" : "comment",
|
|
|
78 |
"Variable name" : "variable-name",
|
|
|
79 |
"Type" : "type",
|
|
|
80 |
"Reference" : "reference",
|
|
|
81 |
"Preprocessor" : "preprocessor",
|
|
|
82 |
"Keyword" : "keyword",
|
|
|
83 |
"Function name" : "function-name",
|
|
|
84 |
"Html tag" : "html-tag",
|
|
|
85 |
"Html italic" : "html-helper-italic",
|
|
|
86 |
"Warning" : "warning",
|
|
|
87 |
"Html bold" : "html-helper-bold"
|
|
|
88 |
},
|
|
|
89 |
context: "pre"
|
|
|
90 |
},
|
|
|
91 |
{ label: "Info:",
|
|
|
92 |
options: { "None" : "",
|
|
|
93 |
"Quote" : "quote",
|
|
|
94 |
"Highlight" : "highlight",
|
|
|
95 |
"Deprecated" : "deprecated"
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
]
|
|
|
99 |
});
|
|
|
100 |
|
|
|
101 |
// add a contextual menu
|
|
|
102 |
editor.registerPlugin("ContextMenu");
|
|
|
103 |
|
|
|
104 |
// load the stylesheet used by our CSS plugin configuration
|
|
|
105 |
editor.config.pageStyle = "@import url(custom.css);";
|
|
|
106 |
|
|
|
107 |
setTimeout(function() {
|
|
|
108 |
editor.generate();
|
|
|
109 |
}, 500);
|
|
|
110 |
return false;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
function insertHTML() {
|
|
|
114 |
var html = prompt("Enter some HTML code here");
|
|
|
115 |
if (html) {
|
|
|
116 |
editor.insertHTML(html);
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
function highlight() {
|
|
|
120 |
editor.surroundHTML('<span style="background-color: yellow">', '</span>');
|
|
|
121 |
}
|
|
|
122 |
</script>
|
|
|
123 |
|
|
|
124 |
</head>
|
|
|
125 |
|
|
|
126 |
<!-- use <body onload="HTMLArea.replaceAll()" if you don't care about
|
|
|
127 |
customizing the editor. It's the easiest way! :) -->
|
|
|
128 |
<body onload="initEditor()">
|
|
|
129 |
|
|
|
130 |
<h1>HTMLArea 3.0</h1>
|
|
|
131 |
|
|
|
132 |
<p>A replacement for <code>TEXTAREA</code> elements. © <a
|
|
|
133 |
href="http://interactivetools.com">InteractiveTools.com</a>, 2003-2004.</p>
|
|
|
134 |
|
|
|
135 |
<form action="test.cgi" method="post" id="edit" name="edit">
|
|
|
136 |
|
|
|
137 |
<textarea id="ta" name="ta" style="width:100%" rows="24" cols="80">
|
|
|
138 |
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 3.2//EN">
|
|
|
139 |
<html>
|
|
|
140 |
|
|
|
141 |
<head>
|
|
|
142 |
<title>Passing parameters to JavaScript code</title>
|
|
|
143 |
<link rel="stylesheet" href="custom.css" />
|
|
|
144 |
</head>
|
|
|
145 |
|
|
|
146 |
<body>
|
|
|
147 |
<h1>Passing parameters to JavaScript code</h1>
|
|
|
148 |
|
|
|
149 |
<p>Sometimes we need to pass parameters to some JavaScript function that we
|
|
|
150 |
wrote ourselves. But sometimes it's simply more convenient to include the
|
|
|
151 |
parameter not in the function call, but in the affected HTML elements.
|
|
|
152 |
Usually, all JavaScript calls affect some element, right? ;-)</p>
|
|
|
153 |
|
|
|
154 |
<p>Well, here's an original way to do it. Or at least, I think it's
|
|
|
155 |
original.</p>
|
|
|
156 |
|
|
|
157 |
<h2>But first...</h2>
|
|
|
158 |
|
|
|
159 |
<p>... an example. Why would I need such thing? I have a JS function that
|
|
|
160 |
is called on <code>BODY</code> <code>onload</code> handler. This function
|
|
|
161 |
tries to retrieve the element with the ID "conttoc" and, if present, it will
|
|
|
162 |
<a href="toc.epl" title="Automatic TOC generation">generate an index</a>.
|
|
|
163 |
The problem is, this function exists in some external JavaScript library
|
|
|
164 |
that it's loaded in page. I only needed to pass the parameter from
|
|
|
165 |
<em>one</em> page. Thus, it makes sense to pass the parameter from the HTML
|
|
|
166 |
code on <em>that</em> page, not to affect the others.</p>
|
|
|
167 |
|
|
|
168 |
<p>The first idea that came to me was to use some attribute, like "id" or
|
|
|
169 |
"class". But "id" was locked already, it <em>had</em> to be "conttoc". Use
|
|
|
170 |
"class"? It's not elegant.. what if I really wanted to give it a class, at
|
|
|
171 |
some point?</p>
|
|
|
172 |
|
|
|
173 |
<h2>The idea</h2>
|
|
|
174 |
|
|
|
175 |
<p>So I thought: what are the HTML elements that do not affect the page
|
|
|
176 |
rendering in any way? Well, comments. I mean, <em>comments</em>, HTML
|
|
|
177 |
comments. You know, like <code>&lt;!-- this is a comment --&gt;</code>.</p>
|
|
|
178 |
|
|
|
179 |
<p>Though comments do not normally affect the way browser renders the page,
|
|
|
180 |
they are still parsed and are part of the DOM, as well as any other node.
|
|
|
181 |
But this mean that we can access comments from JavaScript code, just like we
|
|
|
182 |
access any other element, right? Which means that they <em>can</em> affect
|
|
|
183 |
the way that page finally appears ;-)</p>
|
|
|
184 |
|
|
|
185 |
<h2>The code</h2>
|
|
|
186 |
|
|
|
187 |
<p>The main part was the idea. The code is simple ;-) Suppose we have the
|
|
|
188 |
following HTML code:</p>
|
|
|
189 |
|
|
|
190 |
<pre class="code"><span class="function-name">&lt;</span><span class="html-tag">div</span> <span class="variable-name">id=</span><span class="string">&quot;conttoc&quot;</span><span class="paren-face-match">&gt;</span><span class="function-name">&lt;</span><span class="html-tag">/div</span><span class="function-name">&gt;</span></pre>
|
|
|
191 |
|
|
|
192 |
<p>and our function checks for the presence an element having the ID
|
|
|
193 |
"conttoc", and generates a table of contents into it. Our code will also
|
|
|
194 |
check if the "conttoc" element's first child is a comment node, and if so
|
|
|
195 |
will parse additional parameters from there, for instance, a desired prefix
|
|
|
196 |
for the links that are to be generated into it. Why did I need it? Because
|
|
|
197 |
if the page uses a <code>&lt;base&gt;</code> element to specify the default
|
|
|
198 |
link prefix, then links like "#gen1" generated by the <a href="toc.epl">toc
|
|
|
199 |
generator</a> will not point to that same page as they should, but to the
|
|
|
200 |
page reffered from <code>&lt;base&gt;</code>.</p>
|
|
|
201 |
|
|
|
202 |
<p>So the HTML would now look like this:</p>
|
|
|
203 |
|
|
|
204 |
<pre class="code"><span class="function-name">&lt;</span><span class="html-tag">div</span> <span class="variable-name">id=</span><span class="string">&quot;conttoc&quot;</span><span class="function-name">&gt;</span><span class="comment">&lt;!-- base:link/prefix.html --&gt;</span><span class="paren-face-match">&lt;</span><span class="html-tag">/div</span><span class="paren-face-match">&gt;</span></pre>
|
|
|
205 |
|
|
|
206 |
<p>And our TOC generation function does something like this:</p>
|
|
|
207 |
|
|
|
208 |
<pre class="code"><span class="keyword">var</span> <span class="variable-name">element</span> = getElementById(&quot;<span class="string">conttoc</span>&quot;);
|
|
|
209 |
<span class="keyword">if</span> (element.firstChild &amp;&amp; element.firstChild.nodeType == 8) {
|
|
|
210 |
<span class="comment">// 8 means Node.COMMENT_NODE. We're using numeric values
|
|
|
211 |
</span> <span class="comment">// because IE6 does not support constant names.
|
|
|
212 |
</span> <span class="keyword">var</span> <span class="variable-name">parameters</span> = element.firstChild.data;
|
|
|
213 |
<span class="comment">// at this point &quot;parameters&quot; contains base:link/prefix.html
|
|
|
214 |
</span> <span class="comment">// ...
|
|
|
215 |
</span>}</pre>
|
|
|
216 |
|
|
|
217 |
<p>So we retrieved the value passed to the script from the HTML code. This
|
|
|
218 |
was the goal of this article.</p>
|
|
|
219 |
|
|
|
220 |
<hr />
|
|
|
221 |
<address><a href="http://students.infoiasi.ro/~mishoo/">Mihai Bazon</a></address>
|
|
|
222 |
<!-- hhmts start --> Last modified on Thu Apr 3 20:34:17 2003
|
|
|
223 |
<!-- hhmts end -->
|
|
|
224 |
<!-- doc-lang: English -->
|
|
|
225 |
</body>
|
|
|
226 |
</html>
|
|
|
227 |
</textarea>
|
|
|
228 |
|
|
|
229 |
<p />
|
|
|
230 |
|
|
|
231 |
<input type="submit" name="ok" value=" submit " />
|
|
|
232 |
<input type="button" name="ins" value=" insert html " onclick="return insertHTML();" />
|
|
|
233 |
<input type="button" name="hil" value=" highlight text " onclick="return highlight();" />
|
|
|
234 |
|
|
|
235 |
<a href="javascript:mySubmit()">submit</a>
|
|
|
236 |
|
|
|
237 |
<script type="text/javascript">
|
|
|
238 |
function mySubmit() {
|
|
|
239 |
// document.edit.save.value = "yes";
|
|
|
240 |
document.edit.onsubmit(); // workaround browser bugs.
|
|
|
241 |
document.edit.submit();
|
|
|
242 |
};
|
|
|
243 |
</script>
|
|
|
244 |
|
|
|
245 |
</form>
|
|
|
246 |
|
|
|
247 |
</body>
|
|
|
248 |
</html>
|