2

I have a fairly simple web app built with Delphi (2009) Web Broker. I am trying (and failing) to output Javascript to the page.

The javascript I need to output is in the body of the page in a <form> tag, and is as follows:

<script>DateInput('mydatefield', true, 'DD-MM-YYYY');</script>

This javascript should create a nice date input control (tested ok in a hand-crafted HTML page). The code in my Delphi app this is:

Response.Content := Response.Content + '<script>DateInput(''mydatefield'', true, ''DD-MM-YYYY'');</script>';

The Script tags are being removed from the final output.

As a test I added an HTML button with an onClick event set as follows:

Response.Content := Response.Content + '<input type="button" onClick="alert(''Hello World'')" Value="Hello World"/>';

and when the source of the page is inspected

alert(''Hello World'')

had become

alert(&#39;Hello World&#39;)

I wondered if this translation of the single quote from ' into &#39; might be causing my earlier script tag to be removed?

Any help/pointers appreciated, thanks.

* EDIT

I should point out that the Alert popup does work even with the &#39; characters.

* EDIT

I have tried to use JQuery scripts like $(document).ready( function() { etc }); and it's working, so I guess the problem is not with WebBroker output. Thanks to all who took time to read/think.

Stuart
  • 393
  • 2
  • 9
  • I'm sorry I can't help you too much, however I think that it automatically escapes the single quotes, look at the various properties you can modify, also keep in mind that it's better to split javascript form the html, meaning that in stead of alert('hello world'); you might want to call a function called doHelloWorld(); from a javascript file, otherwise huge head aches can come your way(been there many times). I hope this helps at least a bit. –  May 26 '11 at 16:48
  • Thanks @Dorin - problem is the javascript I need is the "DateInput" which has to be placed within the form element, and already calls the function DateInput with params which I have to dynamically create. – Stuart May 26 '11 at 17:11
  • I would encourage you to have a look at DWScript http://code.google.com/p/dwscript/ and try to combine it with TIdHTTPServer and have a blast (: it's super easy and plesant, of course javascript or javascript libraries that you might/will use could give you some problems sometimes, but above all it's super plesant. –  May 26 '11 at 17:57
  • Thanks Dorin, I'll have a look at it. – Stuart May 26 '11 at 18:44
  • @Dorin - not what I was looking for really. I need a way to output JQuery javascript code from a webbroker app and have it run in the browser. – Stuart Jun 01 '11 at 16:23
  • I'm sorry to read this Stuart, maybe debugging while sending response to the web browser to see where the characters are getting scrambled is the way to go in your case... –  Jun 01 '11 at 18:35
  • Thanks Dorin. Like I said above, jquery stuff is working fine - so maybe there was something amiss with my script tags...? I'll try again soon, it's not a huge priority right now though. :) – Stuart Jun 02 '11 at 07:55

1 Answers1

2

Try HTMLEncode unit HTTPApp:

Response.Content := Response.Content + HTMLEncode('...');
ioan ghip
  • 1,202
  • 2
  • 16
  • 27
  • Not what I was looking for I'm afraid. This encodes all the special characters and removes all the HTML tags entirely. – Stuart Jun 01 '11 at 16:22
  • Going to accept this as an answer for two reasons - my question was a little off, and this answer did prompt me to make sure I was encoding my HTML (and URL's). Thanks. – Stuart Jun 13 '11 at 09:52