Technical Hand Coded Information |
T he somewhat inefficient solution for clients of free web hosting services is to change the HTML to a form which can be used, a JavaScript file, and use the JavaScript file in the web page to "include" those HTML contents. Since JavaScript is an "active" element of a webpage, you can include the same script across into multiple areas of your website, without re-writing or copying the script.
T he shortcomings are that you must keep the original source of HTML on your own computer, saving it for future editing, and each time you wish to upload changes, you must again send the HTML contents into the JavaScript converter.
H owever, for those who need it, Jsinc makes the process easier. It is free, possesses no ads or redirections, and can be customized by yourself.
B asically, a JavaScript "String" can contain any Unicode 16-bit characters. However, the string is quoted between the apostrophes ' and ' ( we will use them; " and " could also be used ), so those characters must be backslashed like this: \'. Also, the backslash itself, if it occurs in a string, must be backslashed, and any newline character must be escaped like this: \n. ( backslashing is also a form of escaping ).
T echnically there are three newline sequences, one of which contains two characters; however, we will work around this by the process of normalization, changing other forms into the standard one.
T he series of steps will then look like this:
replace(/\r\n|\r/g, '\n')
replace(/\\/g, '\\\\')
. Since the
backslash itself is always backslashed, when we
replace one \ with two \\, we actually type code
that includes two \\ and four \\\\.replace(/'/g, '\\\'')
replace(/\n/g, '\\n')
I n a function, those steps would look like this:
function jsstr(s) { s = s.replace(/\r\n|\r/g, '\n'); s = s.replace(/\\/g, '\\\\'); s = s.replace(/'/g, '\\\''); s = s.replace(/\n/g, '\\n'); return '\'' + s + '\''; }
I
n a script, the jsstr("value") of the text value would return that value changed into a workable JavaScript string. If a variable chr had a value of <link rel="stylesheet" href="headings.css" />
, then the result of jsstr(chr) would be '<link rel="stylesheet" href="headings.css" />'.
S ince JavaScript is an active element of a web page, it is also programmable. When you convert your HTML into the script, the script is specially marked so that you can actually put several scripts together, if you desire. If you do so, instead of automatically having the HTML written inside of the page, you can write a program which decides which special HTML file should be included: for example, one for visitors, and one for registered users, or one for morning, one for afternoon, one for evening.
T
o do so you will take more than one JavaScript file and put them together into one file. Each file
originally has a line in it with the mark "// end_var_declaration
." Before you add each JavaScript to the new file, you will remove this line and any lines which are after it. When you are finished with adding as many JavaScript files as you would like into the new file, save the new file, and it will contain several different HTML files, instead of only one.
L ook at this example. The file morning.js originally contained this:
inc_morning_js = '<p>Good morning!</p>\n'; // end_var_declaration document.write(inc_morning_js);
W
hen you put this into the new file, you delete the part beginning with // end_var_declaration
, so the only thing left in the new file is this:
inc_morning_js = '<p>Good morning!</p>\n';
C ontinue doing so with all the JavaScript files that you wish to use.
O ur example file will end by looking like this:
inc_morning_js = '<p>Good morning!</p>\n'; inc_afternoon_js = '<p>Good afternoon!</p>\n'; inc_evening_js = '<p>Good evening!</p>\n';
S ave the new file as something, for example, netstrings.js, and then you insert script tags into each document something like this:
<script type="text/javascript" src="netstrings.js"> </script>
S ince this file is supposed to be for other scripts to use, you should probably insert the script tags into the head section of your HTML documents.
N othing happens at this point, because the new file containing several HTML files is for you to begin programming with. All by itself the script will not do anything. (It is like a library of files, but other scripts can now access the files and show them.)
T he script shown below is called greeting.js, and it can be used in all of your web pages which have the file netstrings.js which contains all of the files morning.js, afternoon.js, and evening.js. It automatically prints the greeting depending on what time of day it is.
<script type="text/javascript"> // greeting.js d = new Date(); hours = d.getHours(); greeting = inc_morning_js; if (hours > 11) greeting = inc_afternoon_js; if (hours > 17) greeting = inc_evening_js; document.write(greeting); </script>
J sinc is written by [ Joseph Myers ], to explain some abilities of JavaScript. His email address is [ cookies@users.sourceforge.net ]. Please report bugs, but nothing else. He can't answer your email, or if he does, he will use qmail from an address which does not exist.
Copyright © Consigliere Ltd., All Rights Reserved. 2001-