Import Text as JavaScript
Let's suppose you have a text file that needs to be imported into a web page. You want to import the text with a line of JavaScript.
A line of JavaScript can't import the text file directly.
But it can be done by using the HTML <script...>
tag to call a PHP script. The PHP script grabs the text file, converts it to JavaScript, and sends the JavaScript to the browser.
The file to import may be plain text or HTML. Video tags and forms may be included. JavaScript may be included. The file may have any valid HTML.
(Ajax could be used to pull in non-JavaScript text. But not with the one-line HTML <script...>
tag this method uses.)
Here is the source code of the PHP script.
<?php /* Text to JavaScript Version 1.0 August 17, 2019 Will Bontrager Software LLC https://www.willmaster.com/ */ header('Content-type: text/javascript'); if( empty($_GET['js']) ) { EchoTextToJavaScript(' *** Inappropriate access. *** '); exit; } $_GET['js'] = str_replace('..','',$_GET['js']); if( preg_match('!^https?://!i',$_GET['js']) ) { EchoTextToJavaScript(file_get_contents($_GET['js'])); } else { EchoTextToJavaScript(file_get_contents("{$_SERVER['DOCUMENT_ROOT']}{$_GET['js']}")); } exit; function EchoTextToJavaScript($text) { foreach( explode("\n",str_replace("\r",'',$text)) as $line ) { $js = str_replace("\\","\\\\",$line); $js = str_replace("'","\\'",$js); $js = str_replace("<!--","<'+'!--",$js); $js = str_replace("-->","--'+'>",$js); $js = preg_replace('/(scr)(ipt)/i','$1\'+\'$2',$js); $js = preg_replace('/(win)(dow)/i','$1\'+\'$2',$js); $js = preg_replace('/(doc)(ument)/i','$1\'+\'$2',$js); $js = preg_replace('/(text)(area)/i','$1\'+\'$2',$js); $js = preg_replace('/(fo)(rm)/i','$1\'+\'$2',$js); $js = preg_replace('/(vi)(deo)/i','$1\'+\'$2',$js); echo "document.writeln('$js');\n"; } } ?>
No customization is required. Just save the source code as text2js.php
(or other .php
file name) and upload it to your server. Now make a note of its URL and you are good to go.
To use it, call text2js.php
with the HTML <script...>
tag. The URL to text2js.php
includes the location of the file to import.
Here is an example HTML <script...>
tag:
<script src="https://example.com/text2js.php?js=location_of_text_file"></script>
In the example HTML <script...>
tag, replace https://example.com/text2js.php
with the URL to the PHP script and replace location_of_text_file
with the location of the text file to be imported.
The location of the text file to be imported may be specified as the server location relative to document root or it may be specified with the text file's http://
or https://
URL. Here is an example of each:
/pages/document.html https://example.com/pages/document.html
The server location method is the URL minus the protocol and domain name. That method of specifying the location of the file to import will work only if the file to be imported is on the same server, at the same domain, as the text2js.php
PHP script.
If the file to import contains PHP code that must be run before it's imported into the web page, then:
-
The file to import must be named with a
.php
file name extension. -
The location of the file to import must be specified with the file's
http://
orhttps://
URL.
Otherwise, the file to be imported may have any file name extension that is recognized as a text file: .txt
, .html
, and .js
are examples.
When the text-to-JavaScript PHP script (source code further above) is installed, it can be called from any of your web pages with one line of JavaScript. In that line of JavaScript, you provide the location of the text file you want to import into your web page.
(This article first appeared with an issue of the Possibilities newsletter.)
Will Bontrager