XF 1.3 JavaScript in BBCode

MS1

Member
Good morning,

I test XenForo under Win7 with XAMPP.
I tried to code additional BBCodes and an error occurs, that I can`t explain to myself.

Target
Create BBCode. Internally JavaScript will be executed. For example: [bb1] something [/ bb1] then leads to an alert box because it was internally programmed: alert ("Hello").

Status
BBCode is programmed (in terms of structure as to: http://xenforo.com/community/thread...b-code-in-xenforo-a-comprehensive-guide.6320/ thank for this great tutorial @Jeremy ).
If I load a discussion in the forum, everything works fine. Depending on how often the BBCode was applied, the alert box appears several times.

Problem
When creating a new post or editing of such, the error message appears:"The server responded with an error. The error message is in the JavaScript console".

I am a little perplexed, because the code of my BBCodes must be error-free?! Otherwise the BBCodes won´t be recognized and converted accordingly my code in a normal page view (reload) without editing or re-post.

Perhaps one of you can explain the mistake?

Thank you
 
Hello Jeremy,

Thank you for your reply. I was so excited that I forgot to upload the pictures.
Information for the pictures: I´ve created one new discussion and inside this discussion I made one post with the bbcode.

Greetings
 

Attachments

  • errorUpdate.webp
    errorUpdate.webp
    186.8 KB · Views: 13
  • ErrorPost.webp
    ErrorPost.webp
    17.3 KB · Views: 11
You can see there that you're actually echoing code out.

At this point, you should most likely just use the built in custom BB code system.
 
Good evening Mike,
When I use the build in bbcode system with an PHP callback function I get the same error !
In the end, I define also only a function which is called when bbcode in the message exists. This function contains Javascript and PHP.

Or have I misunderstood you?
 
Presumably this is a test, but I'm not sure why you need a PHP callback.

But my point was that you're echoing out the HTML/JS. You shouldn't echo; you should return our output from the callback.
 
Ok, so the following code will not work correctly, because the JavaScript-Code is not included in the "return-Statement"?

PHP:
<?php
 public function renderTagbb1(array $tag, array $rendererStates)
  {

?>
<script>
alert("Hello");
</script>

<?php

$output="Executed";
   
  return $output;
     

}
?>
The problem is that I want to execute some Javascript Code to get or set a Cookie (if it not exists) . And after this procedure I check with PHP if this cookie exists.
In dependence of the result of this check I return different things with php.
 
Exactly. You're currently echoing out the javascript. You need to put it into a php string and return it in order to get rid of the error. The correct code may look like this:
PHP:
<?php
public function renderTagbb1(array $tag, array $rendererStates)
  {

$output='
<script>
alert("Hello");
</script>

Executed';

  return $output;
  

}
?>
 
Ok, now I understand the problem! Thanks!
But how can I realise something like the following?

PHP:
<?php
public function renderTagbb1(array $tag, array $rendererStates)
  {?>
<script type="text/javascript">
  createCookie("mycookie",1);
  document.location.reload();
</script>

<?php
    if(isset($_COOKIE["mycookie"]))
        {
          $output= "cookie exists";
       }
       else
       {
    
        $output ="cookie doesn´t exist";
         }

  return $output;
  
  }
?>
For simplicity, I have omitted the JS-Function "createCookie" and other procedures in the JavaScript-Section. But I think this code describes my fundamental problem.
 
If I get it right, you're struggling to understand how Javascript works. Let me give you a brief explanation:

Php is a server-side programming language. Therefor, everything you write in Php will be executed on the machine that handels your server.
Javascript on the other hand is a client-side programming language. Therefor, it will be executed on the computer that your guest visits your site from.

Therefore, the following rules apply:
  • Php will always be fully executed before the document is even handed over to the client
  • Following that, Php can never receive anything from a javascript unless you hand it back from the client to the server which would require an explicit php call for that
  • Javascript will never know ANYTHING about your php-code, not even that it existed in the first place and therefore is unable to work with its variables and stuff.
So, as long as you don't aim on creating an infinit ping-pong-game between client and server, you should write your complete code either in javascript or in php.


By the way, what your javascript currently does is creating a cookie and reloading the page, then creating it again and yet reloading it. You created an infinite loop.
 
Thank you for your detailed answer, @katsulynx !
Basically I want to check with a cookie if the user has JavaScript enabled or not. Because of that I think I need to use both (Php and JS as combination), and check if the cookie exists. Here you can see my full code (which avoids the infinite loop).
PHP:
<?php
  public function renderTagbb1(array $tag, array $rendererStates)
  {
?>


  var myCookie = getCookie2("js");
  if (myCookie == "") {

   createCookie2("js",1);
  document.location.reload();

  }
  else {

  }

</script>
<?php

    if(isset($_COOKIE["js"]))
       {
        //In the future here come other PHP an JS operations.
          $output= "Cookie exists";

       }
       else
       {
       //In the future here come other operations
        $output ="Cookie doesnt exist";


         }

  return $output;

Do I have to put all my Code (also the JS-Code) inside one PHP-Variable ($output) which the function returns?
 
Last edited:
Do I have to put all my Code (also the JS-Code) inside one PHP-Variable ($output) which the function returns?

Yes you do. But still your code doesn't make sense. Currently you're creating a cookie on the client side and that's it, because the php-if-else-query will be executed long before the javascript gets even handed over to the user. You'd need to place the javascript elsewhere. And beside that, a single cookie is not that much of a secure check if javascript is enabled. Your user could disable it after the cookie has been created and still you'd be assuming he has it enabled.
 
Yes, I already thought about this problem with a user which disables JavaScript while already get the cookie. But this weakness I would accept.

katsulynx said:
php-if-else-query will be executed long before the javascript gets even handed over to the user.
I agree with your statement. But this only happens the first time?! Because of the reload of the page, the javascript code was executed and on the next round (after reload) the $_COOKIE["js"] recognizes the cookie.

Edit: I think it would be the best and the cleanest way for me to start a development request and to give the task or job to someone who is an expert at this topic (like you are ;-) )
 
It would work, but it's definitly not a beatiful solution to do a double page load for something as "simple" as that. All in all it seems to be a lot of code for only checking if the user has javascript enabled. What do you even need to know it for?
 
I want to include some Latex inside my new forum with fallback!
I have made the decision that someone must help me with this problem - because I lack the tools. So I think I´ll put my problem to the "Development Requests" to find someone - against payment of course.
If you are interested I could give you further information via a private message. At any rate I want to thank you for your help.

Just out of interest: Are you from Germany? Because I am :)
 
As my profile states, I indeed am :p
However I am lacking the time to do that project for you, still good luck with finding a qualified person! I'm sure there are enough around here. :)
 
  • Like
Reactions: MS1
Top Bottom