This code gets inserted into the page's HTML, by setting it as part of an element's innerHtml. It creates a button-like span that when clicked, opens a JQueryUI window with some constant text and with the text "Thiswindow" below that.
string custom_training_html = "Thiswindow"; // Note this line here.
link = "<span class=\"start_course_button\" onclick=";
link += "javascript:$(\"#temp_test_window\").dialog({minWidth:800,minHeight:600});$(\"#temp_test_custom_html\").html('" + custom_training_html + "');>Test JQueryUI";
link += "</span>\n";
However, if I change the text "Thiswindow" to "This window", I get:
SyntaxError: unterminated string literal
It's the presence of a space, anywhere in this HTML, that breaks the code. Other questions on this site say the problem is line breaks in the HTML, but I haven't got any, I think. I'm just building a string containing the <span> code, and the only difference between a working version and a non-working version is that one contains a space character. What's wrong?
FYI, the window that gets opened looks like this:
<div id="temp_test_window" title="Chaucer's Prologue (The Monk)">
A monk there was, one made for mastery<br />... [snip]
<div id="temp_test_custom_html">Content Goes Here</div>
</div>
EDIT: I've cleaned up the code to put the JS into its own function, but that hasn't solved the problem. In my main .aspx file I have:
function GenerateTestTrainingContent(content)
{
$('#temp_test_window').dialog({minWidth:800,minHeight:600});
$('#temp_test_custom_html').html(content);
}
[and later, outside the <script> tags:]
<div id="temp_test_custom_html">Content Goes Here</div>
And in the .aspx.cs file I have this code creating some HTML as "link":
link = "<span class='start_course_button' onclick=javascript:GenerateTestTrainingContent('OneWord');>";
link += "Test JQueryUI</span>\n";
That works, giving me a link that summons a pop-up JQueryUI window in which the text "OneWord" appears. However, if I change 'OneWord' to 'One Word' in the same code, I get:
SyntaxError: unterminated string literal
javascript:GenerateTestTrainingContent('One
So again, no space = works, any spaces = doesn't work.
onclickattribute. I would also suggest moving the JS code to it's own script block to avoid confusion.