JS: SyntaxError: Unexpected identifier 'bodyText'. Parse error.

Started by Jotade29, March 19, 2024, 08:30:03 PM

Previous topic - Next topic

Jotade29

V: 2.0.19

I have a problem in JavaScript... To my code, i want to add a function programmed in PHP, but ii don't know how to do it. Let me explain, if I add the code it works, but the rest of the code breaks... Giving me the error: Unexpected identifier 'bodyText'. parse error.

What this code does for me is preview a message before being published. I want the preview to read me a programmed bbcode:

This is in Post.template.php
Clean code
// Show the preview section.
                var preview = XMLDoc.getElementsByTagName(\'smf\')[0].getElementsByTagName(\'preview\')[0];
                setInnerHTML(document.getElementById(\'preview_subject\'), preview.getElementsByTagName(\'subject\')[0].firstChild.nodeValue);

                var bodyText = \'\';
                for (var i = 0, n = preview.getElementsByTagName(\'body\')[0].childNodes.length; i < n; i++)
                    bodyText += preview.getElementsByTagName(\'body\')[0].childNodes[i].nodeValue;

                setInnerHTML(document.getElementById(\'preview_body\'), bodyText);
                document.getElementById(\'preview_body\').className = \'post\';

Modify code
// Show the preview section.
                var preview = XMLDoc.getElementsByTagName(\'smf\')[0].getElementsByTagName(\'preview\')[0];
                setInnerHTML(document.getElementById(\'preview_subject\'), preview.getElementsByTagName(\'subject\')[0].firstChild.nodeValue);

                var bodyText = \'\';
\'parse_landa(\'bodyText\');
                for (var i = 0, n = preview.getElementsByTagName(\'body\')[0].childNodes.length; i < n; i++)
                    bodyText += preview.getElementsByTagName(\'body\')[0].childNodes[i].nodeValue;

                setInnerHTML(document.getElementById(\'preview_body\'), bodyText);
                document.getElementById(\'preview_body\').className = \'post\';

With parse_landa('bodyText') (this is a php function) it gives me the format i want, but there is something wrong in the way I put it, which gives me the error:

SyntaxError: Unexpected identifier 'bodyText'. Parse error.

How could I fix it? Thank you!!
Quote from: Diego Andrés on August 12, 2023, 02:20:18 AMI'm afraid convincing Jotade to upgrade to SMF 2.1 will require bigger effort than your work sanitizing Unicode characters  :laugh:

live627

The JS preview function is simply an XML request to the server to get teh already  formatted bbcode. You don't need to modify that bit of JS normally.

Jotade29

Quote from: live627 on March 19, 2024, 10:54:56 PMThe JS preview function is simply an XML request to the server to get teh already  formatted bbcode. You don't need to modify that bit of JS normally.

Thnx Live, how can i to put parse_landa("bodyText"). If i dont put parse_landa() the style doesnt Watch

Thnxxxx
Quote from: Diego Andrés on August 12, 2023, 02:20:18 AMI'm afraid convincing Jotade to upgrade to SMF 2.1 will require bigger effort than your work sanitizing Unicode characters  :laugh:

Arantor


Jotade29

Quote from: Arantor on March 20, 2024, 03:49:56 AMWhat, *exactly* does this function do?

Thank you very much, as always for your attention, @Arantor :)

I was testing an experimental BBC that I was creating, so that it could read URLs from the RAE.

As my bbc is in the experimental phase, for the example of what I want I have used the bbcode glossary, which in the preview does not behave correctly:

https://custom.simplemachines.org/index.php?mod=1525

For it to be displayed in the messages, that is, for it to work correctly I have to do the parse_glossary

$BODYFORPARSEGLOSSARY = parse_glossary($BODYFORPARSEGLOSSARY);
I have to apply that code to the JS of post.template.php, because if I do it in the source of post.php where the preview, which is the part I want to modify, is only displayed correctly if the URL is refreshed. Therefore i have gone to the part of the JS that is responsible for displaying the preview, and i have added that line of code, and in fact it shows it fine, but then the JS stops working, giving me that error. I guess there's something I'm not writing correctly.

The variable that copies the previewed message body in JS is var textBody, and to which i have to do the parse_glossary.

Add that parse_glossary is a function that is in subs.php, so in that JS of post.template i have to invoke it, which i have done in the way i have put in the main message. What i said works for me, but my JS breaks from there giving me the indicated error, when i put

\'parse_glossary(\'bodyText\');
Error:
SyntaxError: Unexpected identifier 'bodyText'. Parse error.

Thnx Arantor!!
Quote from: Diego Andrés on August 12, 2023, 02:20:18 AMI'm afraid convincing Jotade to upgrade to SMF 2.1 will require bigger effort than your work sanitizing Unicode characters  :laugh:

Arantor

Well of course it's a parse error, you can't just jam PHP code inside JavaScript. And even if that did somehow work, you're not passing a *variable* to parse_glossary but a string, which is a totally different thing anyway.

But as ever I still have no idea what you're actually trying to do because you're too busy focusing on how you think you have to do it to explain what you're actually trying to achieve.

Kindred

The glossary has been successfully ported to 2.1 by other means. Use that
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Advertisement: