Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

How to replace the output html content?

Hi:
I'm developing plugin for vanilla.I want to know how to replace the output html content with my plugin?
For example:
I want to add some js code after the html tag,like this:
...

mytestcall();
...

How to implement it?
Thanks!

«1

Answers

  • Options
    wzttest1wzttest1 New
    edited April 2013

    There is some error with this topic,but I can not edit the topic,so I have submit my question again in a right format.
    My question is:

    I'm developing plugin for vanilla.I want to know how to replace the output html content with my plugin? For example: I want to add some js code after the body html tag,like this:
    <body><script type="text/javascript">mytestcall();</script>

    How to implement it?
    Thanks.

  • Options
    vrijvlindervrijvlinder Papillon-Sauvage MVP

    You would add that in the theme's default.master if I understand what you want correctly .

  • Options
    businessdadbusinessdad Stealth contributor MVP

    @wzttest1 You would have to implement an event handler and output the HTML with a simple echo. As far as I know, there is no event that triggers immediately after the <body> tag, therefore you would have to use another one.

    Example

    function DiscussionController_BeforeRenderAsset_Handler($Sender) {
      // Space after the angle bracket left to allow code to be displayed, since 
      // Editor filters out script tags. Such space should be removed in real use case
      echo '< script type="text/javascript">mytestcall();</script>';
    }
    

    This method, however, is not very "orthodox". Unless you really need to output your script in a specific place, scripts are usually loaded with $Sender->AddJs(), which is often called in a Base_Render_Before handler.

  • Options

    @vrijvlinder said:
    You would add that in the theme's default.master if I understand what you want correctly .

    Hi:
    Thank you for your reply.
    I need dynamically get and replace some html code of the output html content in my vanilla plugin,not your said theme I think.

  • Options

    @businessdad said:
    wzttest1 You would have to implement an event handler and output the HTML with a simple echo. As far as I know, there is no event that triggers immediately after the <body> tag, therefore you would have to use another one.

    Example

    function DiscussionController_BeforeRenderAsset_Handler($Sender) {
      // Space after the angle bracket left to allow code to be displayed, since 
      // Editor filters out script tags. Such space should be removed in real use case
      echo '< script type="text/javascript">mytestcall();</script>';
    }
    

    This method, however, is not very "orthodox". Unless you really need to output your script in a specific place, scripts are usually loaded with $Sender->AddJs(), which is often called in a Base_Render_Before handler.

    Hello:
    Thank you for your help.
    I assuredly need insert some js code after the body html tag.
    As your said,we unable to implement it?
    Any help will be appreciative.

  • Options

    btw:
    I need implement it in a DiscussionController_BeforeDiscussionRender_Handler method or a before render method.

  • Options

    @businessdad said:
    wzttest1 You would have to implement an event handler and output the HTML with a simple echo. As far as I know, there is no event that triggers immediately after the <body> tag, therefore you would have to use another one.

    Example

    function DiscussionController_BeforeRenderAsset_Handler($Sender) {
      // Space after the angle bracket left to allow code to be displayed, since 
      // Editor filters out script tags. Such space should be removed in real use case
      echo '< script type="text/javascript">mytestcall();</script>';
    }
    

    This method, however, is not very "orthodox". Unless you really need to output your script in a specific place, scripts are usually loaded with $Sender->AddJs(), which is often called in a Base_Render_Before handler.

    what's the use of your said BeforeRenderAsset and what's the meaning of your said is not very "orthodox"?
    thanks.

  • Options
    businessdadbusinessdad Stealth contributor MVP

    @wzttest1 said:
    what's the use of your said BeforeRenderAsset and what's the meaning of your said is not very "orthodox"? thanks.

    BeforeRenderAsset, as the name implies, is fired before an Asset is rendered. You can search Vanilla code to see where it's fired, or use the Eventi plugin which will show you where the events are fired.

    By "not orthodox" I meant that, when possible, it's usually preferable not to output HTML piecemeal from a Controller. The reason is to avoid messing things up (if a Controller throws out HTML here and there, it becomes difficult to debug). Besides, if one wants to be "anally retentive" about software design, output belongs in the View layer of the MVC.

    This said, I also had to output HTML from the Controller from time to time, and it works fine.

  • Options

    @businessdad said:
    This said, I also had to output HTML from the Controller from time to time, and it works fine.

    You mean the plugin function function DiscussionController_BeforeRenderAsset_Handler(&$Sender) should works well for my plugin?
    Thanks.

  • Options
    businessdadbusinessdad Stealth contributor MVP

    @wzttest1 said:
    You mean the plugin function function DiscussionController_BeforeRenderAsset_Handler(&$Sender) should works well for my plugin? Thanks.

    I can't really say, I don't know your plugin. Try and see if it does what you expect. :)

  • Options

    Hi:
    Sorry.I have tried this code on my plugin,it run the js code many times on one page,and do not run the js after the body tag as I want.
    I think this code is not what I need.
    Is there other way to implement this requirement?

    Vanilla offical team could take a look for this?
    Any help would be appreciative.

    Thank you!

  • Options
    businessdadbusinessdad Stealth contributor MVP

    @wzttest1 said:
    Hi:
    Sorry.I have tried this code on my plugin,it run the js code many times on one page,and do not run the js after the body tag as I want.
    I think this code is not what I need.

    I know it may not be what you need. As I wrote, there is no event that fires immediately after the <body> tag. You might try with the AfterBody event, but I still think that you should use the Eventi plugin to find the most appropriate event.

  • Options

    I need my js code just running after the <body ...> tag instead of before the </body> tag.
    For example:
    <body><script type="text/javascript">mytestcall();</script>

    As your said means there is not other way to implement the requirement?

    Maybe need the vanilla offical guys to help me for this?

    Thank you for your help.

  • Options

    Anybody can help for this?

  • Options
    x00x00 MVP
    edited April 2013

    You should not be putting script in the body anyway, it is bad practice.

    There is an advanced topic that involves deferring all the JavaScript at the end of the body, this if for those that know what they are doing, as it can easily break something, and it is not for individual plug-ins or scripts.

    Even widgets,gadgets, and snippet, can be rewritten so you don't have to directly inject code in the body.

    The only reason why this form persists is lot of advertisers, etc don't want to get their customers to do it properly. or developers do it as a temporary measure and never get round to removing it.

    grep is your friend.

  • Options

    Hi x00:
    Are you vanilla offical guys?
    In my project,assuredly need some js code be added after the body tag,so is there any solution for this?
    Thank you.

  • Options
    hgtonighthgtonight ∞ · New Moderator

    @wzttest1 said:
    Hi x00:
    Are you vanilla offical guys?
    In my project,assuredly need some js code be added after the body tag,so is there any solution for this?
    Thank you.

    This forum is a community of volunteers. Perhaps we could help you find a better 'how' if you explain 'what' you are trying to accomplish.

    Welcome to the community!

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Options

    Hi:
    It's my work need to do.We need accomplish a feature that generate and insert some block html/js codes after the body tag,for example generate a banner on the top of the site.

    Any help would be appreciative.
    Thanks.

  • Options
    hgtonighthgtonight ∞ · New Moderator
    edited April 2013

    @wzttest1 you are able to insert html elements into the DOM at any place using jQuery.

    Something like this will point you in the right direction:

    $(document).ready(function () {
      var Content = '< div id="dynamicbanner">Look at my sweet banner!</div>';
      $('body').prepend(Content);
    });
    

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Options

    Hi:
    Thank you for your help.
    But I need accomplish a feature that run a js function after the body tag.How to do it?

Sign In or Register to comment.