Forums

Need Help Calling Javascript Function (11 posts)

  1. Moessap
    Member
    Posted 2 years ago #

    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 :)!

  2. cdybenko
    Member
    Posted 2 years ago #

    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!

  3. Moessap
    Member
    Posted 2 years ago #

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

  4. cdybenko
    Member
    Posted 2 years ago #

    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.

  5. Moessap
    Member
    Posted 2 years ago #

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

    Thanks again :).

  6. rajanpanicker
    Member
    Posted 2 years ago #

    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

  7. Justin Tadlock
    Member
    Posted 2 years ago #

    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 );
    }
  8. rajanpanicker
    Member
    Posted 2 years ago #

    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.

  9. Justin Tadlock
    Member
    Posted 2 years ago #

    The code I just posted does that.

  10. rajanpanicker
    Member
    Posted 2 years ago #

    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?

  11. magickman5
    Member
    Posted 2 years ago #

    hi rajanpanicker,

    just follow this link

    Want to know more ....

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.