Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I recently added Javascript and CSS files into the wordpress. All the files are being registered through functions.php. One of the files is my_worldmap.js as shown below

$(function() {
    $('#world-map').vectorMap();
});

The issue here, the above function is called as soon as I visit the homepage. My expectation that the code is only called when I visit www.mydomain.com/global-network. How can I do this in WordPress ?

share|improve this question
add comment

2 Answers 2

up vote 0 down vote accepted

Enqueue your script only when the page you want it on is requested:

function wpa_script() {
    if( is_page( 'global-network' ) ){
        wp_enqueue_script(
            'wpa_script',
            get_template_directory_uri() . '/js/my_worldmap.js',
            array('jquery')
        );
    }
}
add_action( 'wp_enqueue_scripts', 'wpa_script' );

See is_page() in Codex.

share|improve this answer
    
This should be it. Thanks for your help –  abiieez Jul 10 '13 at 5:17
    
The url is http://www.mydomain.com/newsite/#/438/global-network-2. I tried with is_page(438) but my js is not called. Any idea ? –  abiieez Jul 10 '13 at 6:11
    
that permalink looks strange, I'll guess your theme is loading pages via ajax & custom queries. it's difficult to say without seeing the theme code, you may need to ask the theme's developer how you can target a specific page load. –  Milo Jul 10 '13 at 6:33
add comment

If you are using wordpress inbuilt jQuery then you should replace your $ sign with jQuery. it should be like this.

jQuery(function() {
    jQuery('#world-map').vectorMap();
});

This should help you.

share|improve this answer
    
I don't see how this changes anything. As I mentioned that the code should run only when I click on a certain page, e.g "Global Map" page –  abiieez Jul 10 '13 at 4:54
    
Have you tried to debug in firebug in firefox. What it is showing there. This might help you. –  Vikas Bhardwaj Jul 10 '13 at 5:10
    
There's no error my friend. It's just the function is called when I visit my homepage, e.g www.mydomain.com. My expectation that the code is only called when I visit www.mydomain.com/global-network. How can I do this in WordPress ? –  abiieez Jul 10 '13 at 5:14
    
I think milo has just answered your question. I didn't get your question in first place. Now its clear. –  Vikas Bhardwaj Jul 10 '13 at 5:19
    
It's alright, thanks for your time :) –  abiieez Jul 10 '13 at 5:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.