Currently I am writing a system for a user that on filling in a form it creates a template for a HTML email that will be able to use with their CRM system, the issue is that you user is a bit docile and for the live of them cannot understand how to view the source of the page so they can copy and paste the template code into their CRM, what I am wanting is there to be able to have a link/button that user can click that will show the source code in a window, I am in now way a javascript developer so any tips, hints examples would be great.
Share
Well you probably want to do something like this: grab the “innerHTML” of the element containing your template HTML (or whatever it is). Then open a new window and drop the HTML into it, surrounded by a
<pre>block. You’ll also want to replace all the important markup characters with appropriate HTML entities, and probably replace newlines with<br>elements.It’d probably be close to this (but it’s not tested):
edit — if you want to strip script blocks, you could add this “replace” call to the series above:
That’s a little shaky because in general trying to recognize HTML with a regular expression is not really possible. However, since script tags don’t really contain markup and definitely end with the next closing script tag, it’s less odious than the general case.