Using Javascript in Forumotion Hitskin_logo Hitskin.com

This is a Hitskin.com skin preview
Install the skinReturn to the skin page

The forum of the forums
Would you like to react to this message? Create an account in a few clicks or log in to continue.
4 posters

    Using Javascript in Forumotion

    avatar
    bmbmjmdm
    Forumember


    Posts : 52
    Reputation : 1
    Language : English

    Solved Using Javascript in Forumotion

    Post by bmbmjmdm September 18th 2014, 7:41 pm

    Hi, so I'm pretty new to HTML/Javascript in general but have extensive Java, C, C++, etc programming background. I'm wondering how I can use javascript in posts for my forum. I tried doing this simple test to try and set and display a variable, however all of the portions inside the <script> tags just got spat out to the post and not actually processed. Am I not understanding something important here?

    Code:
    <html>
    <head>
    <script type="text/javascript">
    var city1 = "Dalas and San Francisco";
    </script>
    </head>
    <body>
    <script type="text/javascript">

     document.write(city1);

    </script>
    </body>
    </html>
    SLGray
    SLGray
    Administrator
    Administrator


    Male Posts : 51554
    Reputation : 3523
    Language : English
    Location : United States

    Solved Re: Using Javascript in Forumotion

    Post by SLGray September 18th 2014, 7:58 pm

    Administration Panel > Modules > HTML/JavaScript
    JavaScript Management



    Using Javascript in Forumotion Slgray10

    When your topic has been solved, ensure you mark the topic solved.
    Never post your email in public.
    Ramdaman
    Ramdaman
    Active Poster


    Male Posts : 1590
    Reputation : 99
    Language : English, Albanian, Macedonian, Russian | HTML, CSS
    Location : New York City

    Solved Re: Using Javascript in Forumotion

    Post by Ramdaman September 18th 2014, 7:59 pm

    2 things.

    #1. Do you have HTML enabled on your forum and prefences?

    #2. I don't think JavaScript works on topics, not too sure though.
    avatar
    bmbmjmdm
    Forumember


    Posts : 52
    Reputation : 1
    Language : English

    Solved Re: Using Javascript in Forumotion

    Post by bmbmjmdm September 18th 2014, 8:08 pm

    Thank you for the quick replies!

    I do have HTML enabled. Also I looked around on here and saw the whole JavaScript Management thing, but how do I use that in actual posts?
    SLGray
    SLGray
    Administrator
    Administrator


    Male Posts : 51554
    Reputation : 3523
    Language : English
    Location : United States

    Solved Re: Using Javascript in Forumotion

    Post by SLGray September 18th 2014, 8:17 pm

    When you create a JavaScript, you will check the in the post option.



    Using Javascript in Forumotion Slgray10

    When your topic has been solved, ensure you mark the topic solved.
    Never post your email in public.
    Ramdaman
    Ramdaman
    Active Poster


    Male Posts : 1590
    Reputation : 99
    Language : English, Albanian, Macedonian, Russian | HTML, CSS
    Location : New York City

    Solved Re: Using Javascript in Forumotion

    Post by Ramdaman September 18th 2014, 8:48 pm

    Make sure you tick the Enable JavaScripts option to yes
    avatar
    bmbmjmdm
    Forumember


    Posts : 52
    Reputation : 1
    Language : English

    Solved Re: Using Javascript in Forumotion

    Post by bmbmjmdm September 18th 2014, 10:35 pm

    Enable JavaScript is checked off. 

    SLGray: I'm not sure what you're referring to. There doesn't appear to be any option to use a javascript when posting a reply.
    Ramdaman
    Ramdaman
    Active Poster


    Male Posts : 1590
    Reputation : 99
    Language : English, Albanian, Macedonian, Russian | HTML, CSS
    Location : New York City

    Solved Re: Using Javascript in Forumotion

    Post by Ramdaman September 18th 2014, 10:43 pm

    Tick JavaScript on and press the green + button to add a JavaScript
    avatar
    bmbmjmdm
    Forumember


    Posts : 52
    Reputation : 1
    Language : English

    Solved Re: Using Javascript in Forumotion

    Post by bmbmjmdm September 18th 2014, 10:46 pm

    As I said, I already enabled javascript and have added my script to the list. The problem I'm having now is actually using those scripts within posts
    SLGray
    SLGray
    Administrator
    Administrator


    Male Posts : 51554
    Reputation : 3523
    Language : English
    Location : United States

    Solved Re: Using Javascript in Forumotion

    Post by SLGray September 18th 2014, 11:19 pm

    When you create a JavaScript, there should be an option for In the Posts at the top.



    Using Javascript in Forumotion Slgray10

    When your topic has been solved, ensure you mark the topic solved.
    Never post your email in public.
    avatar
    bmbmjmdm
    Forumember


    Posts : 52
    Reputation : 1
    Language : English

    Solved Re: Using Javascript in Forumotion

    Post by bmbmjmdm September 18th 2014, 11:28 pm

    Ok, I'm looking at one javascript and I see what you're talking about. I already have "In all the pages" checked off, but I still don't see quite how to use it. Essentially, what I'm wondering is how to display city1 (as seen in my original post) in the body of a post. What this script will eventually do is allow a number to be set inside a post and then have a result based on calculations using that number. For example:


    the javascript will define function(x){return x+1}. The body will then be able to call function(10) and in its place the number 11 should be inserted. 
    SLGray
    SLGray
    Administrator
    Administrator


    Male Posts : 51554
    Reputation : 3523
    Language : English
    Location : United States

    Solved Re: Using Javascript in Forumotion

    Post by SLGray September 18th 2014, 11:41 pm

    I do not see the function in your JavaScript thought.

    It looks like you used HTML coding.



    Using Javascript in Forumotion Slgray10

    When your topic has been solved, ensure you mark the topic solved.
    Never post your email in public.
    avatar
    bmbmjmdm
    Forumember


    Posts : 52
    Reputation : 1
    Language : English

    Solved Re: Using Javascript in Forumotion

    Post by bmbmjmdm September 18th 2014, 11:47 pm

    Yup, I know it's not there. The code I posted is just a test example. I haven't done the real code yet because I want to know how I will be using it first. For the sake of argument though, let's say my Javascript says exactly this:

    Code:
    function mult2(a) { return a * 2;}

    Now let's say in a post I want to use this. How should I do so?
    avatar
    bmbmjmdm
    Forumember


    Posts : 52
    Reputation : 1
    Language : English

    Solved Re: Using Javascript in Forumotion

    Post by bmbmjmdm September 20th 2014, 5:50 pm

    bump cat
    Ramdaman
    Ramdaman
    Active Poster


    Male Posts : 1590
    Reputation : 99
    Language : English, Albanian, Macedonian, Russian | HTML, CSS
    Location : New York City

    Solved Re: Using Javascript in Forumotion

    Post by Ramdaman September 20th 2014, 5:54 pm

    Your javascript should have a function which relates to an existing bbcode. Though I'm not sure if you can create your own bbcodes.

    However in case that is a CSS ruling, you place it in your CSS stylesheet.
    avatar
    bmbmjmdm
    Forumember


    Posts : 52
    Reputation : 1
    Language : English

    Solved Re: Using Javascript in Forumotion

    Post by bmbmjmdm September 20th 2014, 6:03 pm

    Sorry for being so dull, but I'm not sure exactly what you mean :/. How do I relate my function to existing bbcode? All I need it to do is extract a variable and then replace that variable with a block of text (in the example above, that block would simply be the result of an algorithm). I saw on another site that one way to do this is with tag IDs, as shown below;

    Code:
    function mult2() { 
    var num = document.getElementById('NUM').innerHTML;
    var det = "" + num*2;
    document.getElementById('NUM').innerHTML = det;
    }

    And then in the post to do something like this:

    Code:
    <html>
    <body>
    <p id='NUM'>5</p>
    </body>
    </html>

    But the question still remains how to make the actual function run. 

    Also I'm not sure how this relates to CSS either. Again, I apologize, I'm not very familiar with webware ><
    Ramdaman
    Ramdaman
    Active Poster


    Male Posts : 1590
    Reputation : 99
    Language : English, Albanian, Macedonian, Russian | HTML, CSS
    Location : New York City

    Solved Re: Using Javascript in Forumotion

    Post by Ramdaman September 20th 2014, 6:07 pm

    I apologize as I am not too big of a coder. It seems that you want to post your message with HTML via a JavaScript you've already implemented.

    Now, are you completely sure that you have HTML enabled in your preferences? It doesn't matter if you have it enabled in the administration panel.

    Profile => Preferences
    avatar
    bmbmjmdm
    Forumember


    Posts : 52
    Reputation : 1
    Language : English

    Solved Re: Using Javascript in Forumotion

    Post by bmbmjmdm September 20th 2014, 6:09 pm

    Yup, html is on in both my profile and the admin panel
    Ramdaman
    Ramdaman
    Active Poster


    Male Posts : 1590
    Reputation : 99
    Language : English, Albanian, Macedonian, Russian | HTML, CSS
    Location : New York City

    Solved Re: Using Javascript in Forumotion

    Post by Ramdaman September 20th 2014, 6:19 pm

    I'm sorry then, I do not know what to do. You must await for the Forumotion Staff to help you out on this issue.
    Ange Tuteur
    Ange Tuteur
    Forumaster


    Male Posts : 13207
    Reputation : 3000
    Language : English & 日本語
    Location : Pennsylvania

    Solved Re: Using Javascript in Forumotion

    Post by Ange Tuteur September 20th 2014, 8:38 pm

    Hi,

    You cannot format scripts to run in message content. This is restricted for your security to prevent XSS exploits. You can add scripts in the following areas :


    • JS management (Modules > JavaScript Code Management)
    • Templates (Display > Templates), for best usage add scripts to the footer so they run when the doc is ready.
    • Homepage message, for homepage scripts
    • Widgets (Modules > Forum widgets management)
    avatar
    bmbmjmdm
    Forumember


    Posts : 52
    Reputation : 1
    Language : English

    Solved Re: Using Javascript in Forumotion

    Post by bmbmjmdm September 20th 2014, 8:42 pm

    Hm, so is there really no way to do what I'm trying to? I know I've seen scripts that can modify message content (see below), so I feel like there should be a way. 

    https://help.forumotion.com/t115281-how-to-take-off-the-colon-in-spoilers
    Ange Tuteur
    Ange Tuteur
    Forumaster


    Male Posts : 13207
    Reputation : 3000
    Language : English & 日本語
    Location : Pennsylvania

    Solved Re: Using Javascript in Forumotion

    Post by Ange Tuteur September 20th 2014, 8:51 pm

    Are you just wanting to modify the posts with JavaScript ? You should do that here :
    Administration Panel > Modules > JavaScript Codes Management > Create a new script

    Title : Your choice
    Placement : In the topics

    The placement of the script depends where it executes. For example if I wanted to change the background of all posts I could write :
    and stick it only in the topics
    Code:
    $(function() {
      var p = document.getElementsByTagName('DIV');
      for (i=0;i<p.length;i++) if (/post--\d+/.test(p[i].className)) p[i].style.background = 'red';
    });

    I wrap the code with a jQuery Document ready event so the code executes once the DOM is loaded.
    avatar
    bmbmjmdm
    Forumember


    Posts : 52
    Reputation : 1
    Language : English

    Solved Re: Using Javascript in Forumotion

    Post by bmbmjmdm September 21st 2014, 6:12 am

    Yes, thank you! This is what I've been trying to do but I guess I had the syntax slightly off. I do have one question though if you don't mind. If I'm altering the innerhtml of certain blocks, is there any way to add stuff like spoilers/etc to it? The code I have now is 


    Code:
    $(function() { 
    var num = document.getElementById('NUM').innerHTML;
    var det = "[spoiler]" + num + "[/spoiler]";
    document.getElementById('NUM').innerHTML = det;
    })

    This runs fine and replaces any html with the id 'NUM' with the string described by det. However it enters the words [ spoiler ] and [ / spoiler ] rather then actual spoilers.
    Ange Tuteur
    Ange Tuteur
    Forumaster


    Male Posts : 13207
    Reputation : 3000
    Language : English & 日本語
    Location : Pennsylvania

    Solved Re: Using Javascript in Forumotion

    Post by Ange Tuteur September 21st 2014, 4:39 pm

    The bbcodes ([ spoiler ]) are parsed server side, so you'll have to insert HTML instead. I'm not sure how it'll work as I've never tried it, but here's the HTML for the spoiler from phpbb3 :
    Code:
    <dl class="codebox spoiler"><dt style="cursor: pointer;">Spoiler title :</dt><dd><div class="spoiler_content">Spoiler content</div></dd></dl>
    avatar
    bmbmjmdm
    Forumember


    Posts : 52
    Reputation : 1
    Language : English

    Solved Re: Using Javascript in Forumotion

    Post by bmbmjmdm September 21st 2014, 6:14 pm

    Thank you; this works! This solves all my problems, so the topic can be closed. Very Happy  Razz Very good
    Ange Tuteur
    Ange Tuteur
    Forumaster


    Male Posts : 13207
    Reputation : 3000
    Language : English & 日本語
    Location : Pennsylvania

    Solved Re: Using Javascript in Forumotion

    Post by Ange Tuteur September 22nd 2014, 4:46 am

    You're welcome Smile

    Topic archived