The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
Have PHP display actual JavaScript code instead of run it?
Discuss Have PHP display actual JavaScript code instead of run it? in the JavaScript Development forum on Dev Shed. Have PHP display actual JavaScript code instead of run it? JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 1st, 2001, 09:09 PM
|
|
Junior Member
|
|
Join Date: Aug 2001
Posts: 10
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Have PHP display actual JavaScript code instead of run it?
I am trying to make a form that will generate HTML and JavaScript code people can then cut and paste and use on their own Web sites.
The user inputs the necessary information and then submits the form, so far so good, and I know how to make the HTML and JavaScript code based on the variables. What I CAN'T figure out is how to make the php page display the actual HTML and Javascript code verbatim (showing all the tags, etc.) instead of parsing the code and running it.
Suggestions most welcome!!!
|

September 1st, 2001, 09:22 PM
|
|
Contributing User
|
|
Join Date: Jun 2001
Location: Toronto, Ontario, Canada
Posts: 631
Time spent in forums: 7 m 19 sec
Reputation Power: 12
|
|
Lets say you want to display:
<script language="javascript"> .
In html, to display that, you would have to write:
<script language="javascript">
Using php to echo the html:
echo "<script language=\"javascript\">";
Hope this helps 
|

September 1st, 2001, 09:40 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
Or, display it in a text area.
PHP Code:
<html>
<body>
<textarea name="ta1" rows="10" cols="50">
<?php
echo "<div>Hello World</div>\n";
echo "<script type=\"text/javascript\">\n";
echo "alert(\"Goodbye\");\n";
echo '</script>';
?>
</textarea>
</body>
</html>
Last edited by 7stud : September 1st, 2001 at 09:58 PM.
|

September 2nd, 2001, 12:35 AM
|
|
Junior Member
|
|
Join Date: Aug 2001
Location: MN
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Have you tried storing the javascript code in a string - $code, running it through htmlspecialchars() and then doing a preg_replace("/\r\n/","<br>",$code) and finally printing $code out to the browser?
|

September 2nd, 2001, 01:01 AM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
|
Uhhh jacobee,
If you send html or javascript to the browser it executes the code. Running it through any number of functions first will not keep the browser from executing the html that reaches the broswer. Your suggestion turns some of the non-html characters into html. Why do you think the browser is NOT going to execute those <br> tags?
Last edited by 7stud : September 2nd, 2001 at 01:07 AM.
|

September 2nd, 2001, 01:06 AM
|
|
Junior Member
|
|
Join Date: Aug 2001
Location: MN
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
htmlspecialchars() turns the < and > into the appropriate html entities(&lt and &gt), not the other way around. This would prevent any javascript or html from being parsed as such and instead be printed out as plain text. My suggestion for running the $code string through the preg_replace("/\r\n/","<br>",$code) would replace the carriage return/new line characters that wouldn't affect the html output with <br> tags so that you can see the line breaks in the javascript code.
Last edited by jacobee : September 2nd, 2001 at 01:13 AM.
|

September 2nd, 2001, 01:10 AM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
|
Hi,
Well those <br> tags you are putting in the string aren't being run through html special characters are they? Furthermore, if I am a client of the poster, and I cut and paste your new string into my website, is it going to display the html?
|

September 2nd, 2001, 01:14 AM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
|
Ok, I see what you are doing with the \r\n and <br> tags. Will someone cut and pasting the new string into their browser be able to have the html entities execute as html?
|

September 2nd, 2001, 01:16 AM
|
|
Junior Member
|
|
Join Date: Aug 2001
Location: MN
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
You add the <br> after running through htmlspecialchars() otherwise you would end up displaying the line break tags as <br> instead of actually inserting a break into the html flow. If you were to cut and paste the output of what I propose into a .js file for inclusion in an html document (or inside a set of <script></script> for that matter) it shoud run as javascript code because you're not copying any of the underlying HTML markup, just the text which is valid javascript. Does that make sense?
|

September 2nd, 2001, 01:17 AM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
|
Ahhh...I think I understand now. Will the html entities display like html, but they don't execute?
|

September 2nd, 2001, 01:18 AM
|
|
Junior Member
|
|
Join Date: Aug 2001
Location: MN
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
For good measure you might want to stick the modified $code string inside of <pre></pre> in order to preserve any indenting and whatnot. What do you mean by "display like html"?
|

September 2nd, 2001, 01:22 AM
|
|
Junior Member
|
|
Join Date: Aug 2001
Location: MN
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
If you cut and pasted the output of what we're discussing you would get plain text with no html entities present.
|

September 2nd, 2001, 01:23 AM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
|
Like the first reply said:
If you want this html to literally be displayed:
<script language="javascript">
then you send the html entity version to the browser?
I think that must be it. Ok, then I have one suggestion to your suggestion, use str_replace() instead of the regexp, it's faster. Thanks for the lesson!
|

September 2nd, 2001, 01:25 AM
|
|
Junior Member
|
|
Join Date: Aug 2001
Location: MN
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
I guess I'm just used to PERLs regex format, but I think you're right that str_replace() would be faster and more straightforward as well. If the original input that made up the $code variable from above include the <script type='text/javascript'></script> tags then it would display as if you were reading the source file in the browser as opposed to not being able to see the opening and closing source tags.
Thanks for the discussion  - it's always fun.
Last edited by jacobee : September 2nd, 2001 at 01:29 AM.
|

September 2nd, 2001, 01:28 AM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
|
|
|
Hi,
I just tried it out. Thanks, that's pretty neat.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|