Publishing Content From an External File
Pulling the content of an external file into a web page is fairly straight forward. When it can be.
If the content is plain text, with or without HTML markup, and/or it contains PHP code, the content may be pulled in with PHP (assuming your web page file name ends with the .php
file name extension).
Example:
<?php include($_SERVER["DOCUMENT_ROOT"]."/php/content.php") ?>
Replace /php/content.php
with the URI of the content you want pulled into your web page. (The URI is the URL minus the https://
and domain name parts.)
If the external file contains only JavaScript code, it may be pulled in with a script tag somewhat like this.
<script src="https://example.com/js/content.js" type="text/javascript"></script>
Replace https://example.com/js/content.js
with the URL of the external content to pull in and you're good to go. It may be a relative URL (as in /js/content.js
) when the external file is on the same domain as the web page.
But there is a gotcha.
If …
-
Your web page file name does not end with
.php
or there is some other reason you must use JavaScript and not PHP. -
And, the content you want to pull in has at least some content that is not formatted as JavaScript.
… there is still a solution.
The solution is a special PHP script. You'll find the source code below.
The PHP script converts a text file into JavaScript so the content can be published with an HTML script
tag. The file being converted may have its own JavaScript as part of its content.
The solution requires two steps: (1) The PHP script is put on your server. (2) Then, use an HTML script
tag to call the PHP script.
This is the first step: Copy the following PHP script. Name it JSconverter.php
(or other PHP file name that works for you). Upload it to your server where it will be accessible to browsers. Make a note of its URL. (No customization is necessary.)
<?php /* Text to JavaScript Publisher Version 1.0 March 28, 2020 Will Bontrager Software LLC https://www.willmaster.com/ */ file_put_contents('js_'.time().'.txt',"{$_SERVER['DOCUMENT_ROOT']}{$_GET['file']}"); if( empty($_GET['file']) ) { exit; } foreach( explode("\n",str_replace("\r",'',file_get_contents("{$_SERVER['DOCUMENT_ROOT']}{$_GET['file']}"))) 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); echo "document.writeln('$js');\n"; } ?>
To pull in content via JSconverter.php
, specify the URL of JSconverter.php
in your JavaScript followed by a parameter that specifies the file to convert and publish.
The second step is putting the HTML script
tag on your web page where you want content from an external file published.
Let's assume you uploaded JSconverter.php
to https://example.com/JSconverter.php
(colored blue for easier identification in the script
tag example further below).
Let's further assume that the URI of the file to pick up, convert to JavaScript, and publish on the web page is /content.html
(colored red for easier identification in the script
tag example below).
With that information, this is the HTML script
tag.
<script src="https://example.com/JSconverter.php?file=/content.html' type="text/javascript"></script>
Update https://example.com/JSconverter.php
to the URL of your JSconverter.php
file. And update /content.html
for the URI of the content you want to pull in.
Review
For web pages where PHP can be used to pull in content, PHP can be used to pull in any text content. It doesn't have to contain PHP code.
JavaScript can be used on any web page. What it pulls in, though, must be correctly formatted JavaScript.
If it is not correctly formatted JavaScript, the above PHP script may be installed on the server to do the JavaScript formatting before it is pulled into the web page.
Except for unusual cases, one of the above techniques should suffice for publishing the extra content you need pulled into the web page.
(This article first appeared with an issue of the Possibilities newsletter.)
Will Bontrager