• I found a javascript code I’d really like to use, but can’t find out how to implement it.

    I don’t know anything really about javascript, but from what I understand, you create some javascript function which goes in the <head> section of the html code, like this:

    <script language=”javascript”>
    “function”
    </script>

    Then you call the function in the body of the html file to use it. I’m not sure how to use it with how the blog is set up though. I found a folder titled “scripts” in my theme folder, so I created the script and uploaded it there, but this doesn’t work. I also tried putting the script in “page.php”, but this doesn’t work either.

    Anyone have any ideas?

    Thanks… I appreciate any help :)!

Viewing 10 replies - 1 through 10 (of 10 total)
  • Try linking to the javascript file in the head of your page.php document

    <html>
    <head>
    <script type="text/javascript" src="<yoursite>/wp-content/themes/theme_name/scripts/yourjavascript.js"></script>
    
    </head>
    <body>
    </body>
    </html>

    Then try calling the javascript functions from anywhere in your page.php file.

    Also try a theme that uses javascript and see how they link to the script source and then use it in the theme.

    Good luck!

    Thread Starter moessap

    (@moessap)

    Hmmm ok… thanks a lot for the help :)!

    I hope I didn’t make it sound too confusing.

    Sometimes it’s easiest to learn by downloading a theme that uses javascript and seeing how they link to the javacript source file and call it from their php files.

    There are lots of introduction to javascript tutorials online if you google for them.

    Thread Starter moessap

    (@moessap)

    I’m a little confused actually, lol… but I usually figure things out through trial and error… and google ya.

    Thanks again :).

    My theme dont have a scripts directory.But it does have a js directory.I believe I should be copying the files into that cricket.
    And could you make it a bit more specific as my single.php is where I need to display my javascript code.Here my single.php has elements that start with

    <?php get_header(); ?>
    <?php
    global $options;
    ………………..

    So here what and where shall I add my .js function call.
    Also my webhost has a htdocs and inside that lies all the themes and other things.So what should be the path?
    <script type=”text/javascript” src=”<www.mysite.com/>confusedpath/yourjavascript.js”></script>
    Thanks in advance

    Please never add scripts to your theme’s header file. WordPress has a function for this: wp_enqueue_script(). Assuming your script is in your theme’s /js folder, add this code to your theme’s functions.php:

    add_action( 'template_redirect', 'load_my_js' );
    
    function load_my_js() {
    
    	/* Only load for single posts. */
    	if ( is_single() )
    		wp_enqueue_script( 'my-js', get_stylesheet_directory_uri() . '/js/yourjavascript.js', false, 0.1 );
    }

    What if I want to display the script only in my posts and not in pages and home page?Then what I should I do?I tried inserting in single.php but nothing is happening.So if I include the above script in functions.php,it will be displayed on all my pages and posts right.I dont want that,I want it to be displayed only on my posts.Anyway thanks in advance.

    The code I just posted does that.

    No its not working.I coped the script in js directory,then copied this code randomly inside functions.php but still its of no use.Or should I name the path instead of /js/yourjavascript.js
    if I have a htdocs,that must mean that I should incude that also in the path right?

    hi rajanpanicker,

    just follow this link

    Want to know more ….

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Need Help Calling Javascript Function’ is closed to new replies.