JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 1st, 2001, 09:09 PM
borntorun borntorun is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Posts: 10 borntorun User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!!!

Reply With Quote
  #2  
Old September 1st, 2001, 09:22 PM
DJdrenaline DJdrenaline is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2001
Location: Toronto, Ontario, Canada
Posts: 631 DJdrenaline User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #3  
Old September 1st, 2001, 09:40 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
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.

Reply With Quote
  #4  
Old September 2nd, 2001, 12:35 AM
jacobee jacobee is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Location: MN
Posts: 7 jacobee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #5  
Old September 2nd, 2001, 01:01 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
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.

Reply With Quote
  #6  
Old September 2nd, 2001, 01:06 AM
jacobee jacobee is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Location: MN
Posts: 7 jacobee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
htmlspecialchars() turns the &lt and &gt into the appropriate html entities(&amplt and &ampgt), 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 &ltbr&gt tags so that you can see the line breaks in the javascript code.

Last edited by jacobee : September 2nd, 2001 at 01:13 AM.

Reply With Quote
  #7  
Old September 2nd, 2001, 01:10 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
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?

Reply With Quote
  #8  
Old September 2nd, 2001, 01:14 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
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?

Reply With Quote
  #9  
Old September 2nd, 2001, 01:16 AM
jacobee jacobee is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Location: MN
Posts: 7 jacobee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
You add the &ltbr&gt after running through htmlspecialchars() otherwise you would end up displaying the line break tags as &ltbr&gt 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 &ltscript&gt&lt/script&gt 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?

Reply With Quote
  #10  
Old September 2nd, 2001, 01:17 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
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?

Reply With Quote
  #11  
Old September 2nd, 2001, 01:18 AM
jacobee jacobee is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Location: MN
Posts: 7 jacobee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
For good measure you might want to stick the modified $code string inside of &ltpre&gt&lt/pre&gt in order to preserve any indenting and whatnot. What do you mean by "display like html"?

Reply With Quote
  #12  
Old September 2nd, 2001, 01:22 AM
jacobee jacobee is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Location: MN
Posts: 7 jacobee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #13  
Old September 2nd, 2001, 01:23 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
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!

Reply With Quote
  #14  
Old September 2nd, 2001, 01:25 AM
jacobee jacobee is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2001
Location: MN
Posts: 7 jacobee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 &ltscript type='text/javascript'&gt&lt/script&gt 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.

Reply With Quote
  #15  
Old September 2nd, 2001, 01:28 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
Hi,

I just tried it out. Thanks, that's pretty neat.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Have PHP display actual JavaScript code instead of run it?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap