burger menu icon
WillMaster

WillMaster > LibraryWeb Content Preparation

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!

Viewing HTML Source Code

Viewing HTML source code with a browser means viewing the source code as sent to the browser. PHP and other server-side processes are completed before sending the page to the browser.

After the browser gets the web page from the server, there may be additional changes to the source code. Generally, those additional changes would be done with JavaScript.

Therefore, viewing HTML source code could be different depending on when and how it is viewed.

There are several ways to view the source code of a web page.

  • With the browser's "view source" menu item. This might or might not be satisfactory. Recently loaded content might not be included. This works only for the web page currently loaded in the browser. There is no source code for this. Simply use your browser's "view source" menu item.

  • With a PHP code snippet (source code provided further below). It typically will provide the exact source code that the server would send to a browser. This works for any non-restricted URL.

  • With JavaScript for the current web page (source code provided further below). This works only for the web page where the JavaScript is located.

PHP Code Snipped

Here is a PHP code snippet for viewing the source code at any non-restricted URL.

<?php echo('<pre>'.htmlspecialchars(file_get_contents('https://example.com/')).'</pre>'); ?>

In the above source code snippet, replace https://example.com/ with the URL you are targeting. Put the snippet on a test PHP web page. Load the page URL into your browser.

Javascript Code

The JavaScript below needs to be placed below the cancel </HTML> tag. After the web page is loaded, it will display the source code of the content between the HTML tags.

<!-- (Only the content between the HTML tags is displayed, not the HTML tag itself.) -->
<pre id="my-pre-tag"></pre>
<!-- JavaScript should be under the </html> tag, not anywhere within it. -->
<script type="text/javascript">
function DisplayTheSourceCode()
{
   var tag = document.getElementsByTagName("HTML")[0];
   var html = String(tag.innerHTML);
   html = html.replace(/\&amp;/g,"&amp;amp;");
   html = html.replace(/\</g,"&amp;lt;");
   html = html.replace(/\>/g,"&amp;gt;");
   document.getElementById("my-pre-tag").innerHTML = html;
}
setTimeout(DisplayTheSourceCode,5000); // Adjust 5000 to your preferred number of milliseconds to wait.
</script>

The above will display only the web page where the JavaScript is located. It may be useful when debugging a PHP-generated web page. The JavaScript will show the code the browser currently is working with.

The source code will be between pre tags. If you change the my-pre-tag id of the pre tag, you'll need to make a corresponding change in the JavaScript toward the bottom of the source code (4 lines up from the bottom).

The JavaScript runs after the web page loads. The above JavaScript delays 5000 milliseconds (5 seconds) before obtaining the source code. You may adjust that number in the above source code (near the bottom) to your preferred number of milliseconds.

There you are, three ways to view web page source code.

(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-2026 Will Bontrager Software LLC