How to Use JavaScript in WordPress?

Discussion in 'WordPress' started by Kaabi, Apr 3, 2013.

  1. #1
    Does anyone know how to activate the use of JavaScript in WordPress, so it can be added directly to the content of pages? So far none of the plug-ins that have been tried are working. Thank you!
     
    Kaabi, Apr 3, 2013 IP
  2. MilesWeb

    MilesWeb Well-Known Member

    Messages:
    869
    Likes Received:
    35
    Best Answers:
    7
    Trophy Points:
    173
    #2
    A plugin might not help in this case. You will have to create a .js file and upload it on to the server. You may create a folder name scripts and upload the myjavascript.js file in it. You can call the javascript through WordPress posts and pages. Switch to HTML mode in your WordPress editor and enter the code
    <script type="text/javascript" src="http://www.yourdomain.com/scripts/myjavascript.js"></script>
    PHP:
    in your post it will execute the .js file. Let me know if it doesn't solves your issue.
     
    MilesWeb, Apr 5, 2013 IP
  3. Sayedtaqui

    Sayedtaqui Greenhorn

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #3
    Ahh! Javascript can be used anywhere on any normal php page of wordpress. You just have to open the <script> tag and it will work like it works outside of wordpress. You can also create a external javascript file and attach it to the header like MilesGeek said and it will work fine. But if you want to use the wordpress standard way of attaching javascript then you would need to tell wordpress that you are attaching a javascript file by enqueing the file like this

    php wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ); ?>
    Code (markup):
    You can read more about it , refer to this article http://codex.wordpress.org/Function_Reference/wp_enqueue_script

    But if you are not creating a theme or plugin and you have no plan to submit it to wordpress for review. dont worry about enqueing just do it the normal way.
     
    Sayedtaqui, Apr 5, 2013 IP