Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
0 votes
1.0k views
in Q2A Core by
edited by

When doing some testing to find out about the still-persisting-q2a-header-error I had to change qa-theme-base.php, line 106
  from: echo str_repeat("\t", max(0, $this->indent)).$html."\n";
  to: echo $html;
 
The effect: HTML code does not get indented anymore. Example:

    <p>test</p>
          <p>indent</p>

Becomes: <p>test</p> <p>indent</p>

-
Doing so I discoverd that q2a core has not correct javascript syntax as CKEditor did not load anymore! After "disabling" the indent, Javascript could not be executed/parsed properly. Mostly because statements were not terminated by a semi-colon.

The following changes of 7 files are necessary to have correct JS syntax - I ask gidgreen to implement these corrections in v1.5.5 or v1.6:

1. qa-page-question-view.php

about line 850:
    $onloads[]='document.getElementById('.qa_js($formid).').qa_show=function() { '.$captchaloadscript.' };';  // added missing ; in the end

  line 855:
  $onloads[]='document.getElementById('.qa_js($formid).').qa_load=function() { '.$editor->load_script('a_content').' };'; // added missing ; in the end

  line 872:
  $onloads[]='document.getElementById('.qa_js($formid).').qa_focus=function() { '.$editor->focus_script('a_content').' };'; // added missing ; in the end

about line 976:
  $onloads[]='document.getElementById('.qa_js($formid).').qa_show=function() { '.$captchaloadscript.' };'; // added missing ; in the end
  line 981:
  $onloads[]='document.getElementById('.qa_js($formid).').qa_load=function() { '.$editor->load_script($prefix.'content').' };'; // added missing ; in the end
  line 983:
  $onloads[]='document.getElementById('.qa_js($formid).').qa_focus=function() { '.$editor->focus_script($prefix.'content').' };'; // added missing ; in the end

2. qa-page-question.php
line 246:
  "qa_element_revealed=document.getElementById('anew');" // added missing ;

3. qa-page.php
line 271: $script=array('<SCRIPT TYPE="text/javascript"><!--');
   to: $script=array('<SCRIPT TYPE="text/javascript">'); // removed <!--
line 311: $script[]='//--></SCRIPT>';
   to: $script[]='</SCRIPT>'; // removed: //-->
line 297+298:
  "\tif (typeof qa_oldonload=='function')",
  "\t\tqa_oldonload();"
to: "\tif (typeof qa_oldonload=='function') {",
    "\t\tqa_oldonload(); }" // added {}
    
4. qa-theme-base.php
about line 330: '<SCRIPT TYPE="text/javascript"><!--',
  to: '<SCRIPT TYPE="text/javascript">', // removed <!--
  line 333:
  to: '</SCRIPT>' // removed //--></SCRIPT>

5. qa-app-format.php
  line 1167: $funcscript[]="};"; // added missing ;

6. qa-recaptcha-captcha.php
  line 123 (line after "\tlang:".qa_js($language),):
  "};", // added missing ;

7. qa-wysiwyg-editor.php
about line 184 / line after: ($uploadall ? (", filebrowserUploadUrl:".qa_js(qa_path('wysiwyg-editor-upload'))) : "").
  "};" // added missing ;
 

Just after those changes Javascript could be parsed correctly, even on one line.

It is important to note that in JS functions do not need to be determined by semi-colon, however, the lines above are statements: var x = function() {...}; and not functions.

Q2A version: 1.5.3

2 Answers

0 votes
by
I will look into this, but Javascript does not require semicolons to end statements.
by
I thought so as well but having all javascript on one line, it did not work, giving me a "missing ; before statement" error several times. That's why I had to fix it (like Sherlock Homes) and posted the result above.

See also http://stackoverflow.com/a/444082 "Yes, you should use semicolons after every statement in JavaScript." + 2nd answer over there!

As you used new lines for js-statements in q2a code, Javascript was "inserting" the semicolons for you, which is not the case on one line...
by
edited by
Missing semicolons can cause all kinds of problems because you are relying on the new lines to terminate statements. If you are generating Javascript with PHP and miss a new line (easy to do), something will get broken.

Also, Kai is correct about the HTML comments too. All browsers in use for the past 10 years understand what a script tag is (even if they don't run Javascript). HTML5 also says the "type" attribute on the script tag is not necessary (again, all browsers interpret <script> as JS anyway).
0 votes
by

I have fixed all the semicolons I could find in Q2A 1.6-dev (some were in slightly different places from what you mentioned above, I believe.) If you get a chance please download it and tell me if it is OK now.

by
v1.6.2: You forgot to set one semicolon in qa-app-format.php, line 1165.
$funcscript[]="};";
by
And please add the brackets for javascript in qa-page.php at:
    "\tif (typeof qa_oldonload=='function') {", // added {}
    "\t\tqa_oldonload(); }"
Otherwise, when minifying, JS will take all following code into the function's scope.
by
I checked into these additional corrections, but don't understand the need for them. Of course they don't do any harm anyway, but I want to understand.

Your first suggestion refers to the closing of a function { ... } block and I didn't think they needed semicolons because they are not statements.

Your second suggestion refers to adding curly brackets around a single statement which is after an if (...). I don't see what this has to do with line endings and I would think that a minifying tool should do the right thing?
by
The issue was, when I minified the JS code (I used http://www.refresh-sf.com/yui/ ) the js code did not work anymore. That's why I was concerned about adding ; and {} for minified versions.
...