开发者

Local host not letting my link to javascript in wordpress

I'm more of a designer by trade, but I can usually hold my own in HTML,CSS, and Javascript.

This problem has been bugging me for a few days though. I'm developing a new wordpress site for myself using MAMP on OS X and I'm trying to add some jQuery magic to spice some things up.

For some reason I can't get the files to be read. Firebug says everything is fine, and the javascript seems to be embedded but the actual functions aren't getting called.

When I paste the raw javascript into <script> tags right into the <head> everything is fine. But once I try calling them with

<script type="text/javascript" src="#"></script>

The functions aren't called. I've tried linking to JS in my JS folder, I've tried linking to google hosted JS, and even others who are usi开发者_如何学Pythonng the same jQuery plugin's JS to test.

I can't figure it out. I can't figure out if it's a wordpress thing or a localhost thing. Any ideas guys?

Thanks!

-Will


The "src" attribute has to be an actual URL, or at least a path "tail" relative to the origin of the page or the server. The string "#" doesn't mean anything.

Also, you don't really need the "type" attribute, but I understand that HTML 4 and XHTML people seem to like it so whatever.

Thus something like:

<script src='/scripts/something.js'></script>

or

<script src='./scripts/something.js'></script>

or a variation depending on how you arrange files on your server.


when working with wordpress you should include all of your js calls in the themes function.php. if you google for wp_enqueue_script you'll find lots of resources. here's a way to go with jquery on googles CDN:

function tm_javascript() {
if (!is_admin()) {
    wp_deregister_script('jquery');
    wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js', false, '1.4.2', true);
    wp_enqueue_script('jquery');

    // load a JS file from my theme: js/theme.js
    wp_enqueue_script('tm_filter', get_bloginfo('template_url') . '/js/filterable.js', array('jquery'), false, true);
    wp_enqueue_script('tm_hash', get_bloginfo('template_url') . '/js/jquery.ba-hashchange.min.js', array('jquery'), false, true);
    wp_enqueue_script('tm_ui', get_bloginfo('template_url') . '/js/jquery-ui-widget.js', array('jquery'), false, true);
    wp_enqueue_script('tm_scroll', get_bloginfo('template_url') . '/js/jquery.smoothDivScroll-1.1-min.js', array('jquery', 'tm_ui'), false, true);      
    wp_enqueue_script('tm_ajaxaks', get_bloginfo('template_url') . '/js/aks.js', array('jquery', 'tm_hash', 'tm_filter', 'tm_ui', 'tm_scroll'), false, true);   
}
}
add_action('init', 'tm_javascript');

the fridst parts deregisters the included jquery from wordpress and then loads the actual 1.4.2 version. you can modify the code to load always the newest verion etc. jsut google it up.

then, you can load your theme specific js files from the themes subfolder /js/ and the really usefull part is the array, where you can declare dependencies. ex. the smoothDicScroll plugin needs to be called after jquery and the tm_ui (ui-widget) plugin.

in the end you call all the js scripts in the footer..

worls live and on localhost with MAMP. it's nice to include a fallback for a local jquery version when developing on localhost, since sometimes you're not connected to the internet.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜