WordPress.org

Ready to get started?Download WordPress

Forums

How to prevent Page Edit from changing javascript code (5 posts)

  1. lzr0
    Member
    Posted 2 years ago #

    Hi,
    I am trying to add a javascript calculator to a page. I am pasting the script from Edit Page>html view. When I update and view the page I see the editor changed some characters, which made script inoperative.
    Particularly it encodes "&" and ">" characters.
    I read elsewhere that if put we put javascrpipt within

    /* <![CDATA[ */
    ...
    /* ]]> */

    it should work in xhtml, put apparently it does not.
    Is there anything can be about with it?

  2. You really can't reliably do that and still use the built in TinyMCE editor. While using that editor works for almost all WordPress users, it does mung any javascript and HTML that is inserted by hand.

    You can try creating a custom page template. That way the javascript isn't in the editor and you just apply that template to your page.

    http://codex.wordpress.org/Pages#Page_Templates

    Or if the javascript is a always the same, try creating a short code plugin. You used the short code in the editor and that javascript gets put in place of the short code when the page is displayed.

    http://codex.wordpress.org/Function_Reference/add_shortcode

  3. Patrick Nommensen
    Member
    Posted 2 years ago #

    Here's some good information to start with: http://codex.wordpress.org/Using_Javascript

    To include a Javascript inside a post, you need to combine both the call to the script file with the call to the Javascript itself.

    <script type="text/javascript" src="/scripts/updatepage.js"></script>
    <script type="text/javascript">
    <!--
    updatepage();
    //--></script>

  4. lzr0
    Member
    Posted 2 years ago #

    You can try creating a custom page template. That way the javascript isn't in the editor and you just apply that template to your page.

    Jan, thanks. I will need to digest this. My knowledge of WP is very limited.

    @Patrick

    Here's some good information to start with: http://codex.wordpress.org/Using_Javascript

    This codex topic relates to calling out an external js file, I was asking about pasting script directly into the html.

    However, now I see, putting a js in an external file probably would be simpler. Except, the instructions in the above link are unclear.

    <script type="text/javascript" src="/scripts/updatepage.js"></script>
    <script type="text/javascript">
    <!--
    updatepage();
    //--></script>

    Is update.js just an example of js which I can change to myscript.js, or it is a WP file which I need to call besides myscript.js? If it has to be a WP file, it does not exist in my wp-admin/js folder. Also, the codex topic states:

    Make sure that each script is defined by its function name such as:
    function updatepage(){var m="Page updated "+document.lastMo.......}

    It's unclear what to put instead of these dots?
    Thanks folks for your time.

  5. lzr0
    Member
    Posted 2 years ago #

    I am trying to add a javascript calculator to a page. I am pasting the script from Edit Page>html view. When I update and view the page I see the editor changed some characters, which made script inoperative.
    Particularly it encodes "&" and ">" characters...

    Just for information, I found a solution, although it may not work in all themes. When I paste a javascript in a text widget, WP does not modify it, and it seems to work fine. The theme I use (suffusions) allows users to put a special widget (called ad-hoc widget) in any place in the main content area by calling the id of this widget. This lets me put a js anywhere. Don't know if other themes have such feature though, but it works in my case.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.