Embedded JavaScript doesn't work?
Well, I'm working on Wordpress Theme and I've a very strange behavior.
When I put my jQuery script in script tag (anywhere on the site) it works like charm.
But when I try to link to a file 开发者_运维百科with the same script, like:
<script src="address" type="text/javascript"></script>
- it doesn't.
What's wrong? The link is VALID. The script was tested in the beginning but also at the end of a site (so that's no "on load" problem).
How to troubleshoot problems like that? Is there a way to debug JavaScript in Opera?
Can't provide a link.
Thanks
a javascript file should ends with .js
, probably path and especially file name is wrong
take a look at firebug (or opera console) if this get returns a 404 error
Like @Fabrizio Calderan said, maybe the script URL is wrong? Or maybe because of lack of js
extension server doesn't send proper mime type and browser has a problem with interpreting the file.
Also if it's wordpress, you probably rewrite post's URLs somehow - if the script is in your domain's root directory you should put /
before filename, if it's in a template directory try what @Nick Pyett proposed.
Yes, you can debug JavaScript in Opera - check out Opera Dragonfly
You need to use the wp_enqueue_script() function. More here http://codex.wordpress.org/Function_Reference/wp_enqueue_script
Unfortunately you all were wrong, the answer was:
$(something) works only inline and when embedded - Wordpress accepts only jQuery instead of the dollar sign, so everything works fine after changing all "$" to "jQuery" :)
Anyways thanks! :)
Assuming your script is a .js file, is in your template folder and the line is being included between the html head tags...
<script src="<?php echo get_bloginfo('template_url')?>/address.js" type="text/javascript"></script>
Check the trailing slash after the php because i can't remember if the function includes one off the top of my head.
If you go to View - Developer Tools and check Opera Dragonfly, you will see Dragonfly open in the bottom of the browser, which appears to have similar capabilities to Firebug for Firefox (DOM browser, Network monitor, Script debugger, etc). This may help you get to the root of your problem.
精彩评论