I want my Javascript code to be loaded, only when #example DIV exists on page. (e.g: load video.js code, only when there is a video on the page) How to do that?
So how I can detect the presence of the div without the help of the Jquery? (I know it's possible with wordpress 'Custom Fields', but don't know how to do that) Did I understood your purpose?
@_John_ Thanks, I will wait for your solution. @filipeb Didn't worked for me. What's the 'head' tag name? and is it same for everywhere or should I change that in my page?
The reason why it didn't work might be because the script itself isn't a DOM element - check out that thread I linked to to see how to make it into one.
Also check this out: http://api.jquery.com/jQuery.getScript/ Seems like this could be a more semantic approach to dynamically using js files. I hope this helped.
@TheDoc Yes, it looks you are right, I didn't saw a Script as a DOM ever. Thanks for you helps. @_John_ Thanks a lot, I used your first code & that worked great! The second one is not suitable for me, because I don't need those messages (e.g failed). Thanks again.
How to do that?
Did I understood your purpose?
?
Doesn't worked. What's wrong?
http://stackoverflow.com/questions/1900874/how-to-add-anything-in-head-through-jquery-javascript
Skip the jQuery answer and find the JS one.
Ok?
I didn't test it, but that should be what you're looking for. I just used the method posted through TheDoc's comment.
@filipeb Didn't worked for me. What's the 'head' tag name? and is it same for everywhere or should I change that in my page?
<head>, that's where you want to append your script to.Demo: http://jsfiddle.net/john_motyl/McNTG/
Not sure if this is the best way of doing this, but it works.
Also check this out: http://api.jquery.com/jQuery.getScript/
Seems like this could be a more semantic approach to dynamically using js files. I hope this helped.
This works as well and is pretty cool:
I feel that the
.fail()is acting like atry@_John_ Thanks a lot, I used your first code & that worked great! The second one is not suitable for me, because I don't need those messages (e.g failed).
Thanks again.