Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWebsite Owner Tools

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

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:

  1. The file to import must be named with a .php file name extension.

  2. The location of the file to import must be specified with the file's http:// or https:// 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

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC