Home › Forums › JavaScript › Load my .js code, only when ‘x’ DIV exists
- This topic is empty.
-
AuthorPosts
-
June 19, 2012 at 11:42 am #38571
farzadina
ParticipantI 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?June 19, 2012 at 12:02 pm #104587Paulie_D
MemberI think this is redundant as you would need JS to detect the presence of the div anyway…wouldn’t you?
June 20, 2012 at 3:03 am #104640farzadina
ParticipantSo 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?June 20, 2012 at 3:56 am #104642filipeb
Member
if (document.getElementById('divID')) {
//do something
}
?
June 20, 2012 at 3:43 pm #104679farzadina
Participant
}
Doesn’t worked. What’s wrong?
June 20, 2012 at 4:09 pm #104682TheDoc
MemberThat’s because you’re just writing a bunch of text, it doesn’t know what to do with it.
http://stackoverflow.com/questions/1900874/how-to-add-anything-in-head-through-jquery-javascript
Skip the jQuery answer and find the JS one.
June 21, 2012 at 10:29 am #104704farzadina
ParticipantSorry @TheDoc, I couldn’t understand your purpose. My question is: Appent X, when have Y = Don’t append X when there is no Y DIV.
Ok?June 21, 2012 at 11:20 am #104705JohnMotylJr
Participant— deleted because it was garbage code —
June 21, 2012 at 11:47 am #104709JohnMotylJr
Participant— deleted because it was garbage code —
June 21, 2012 at 11:49 am #104710filipeb
Member
";
document.getElementsByTagName('head')[0].appendChild( toAppend );
}
I didn’t test it, but that should be what you’re looking for. I just used the method posted through TheDoc‘s comment.
June 21, 2012 at 12:13 pm #104712farzadina
Participant@_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?June 21, 2012 at 1:53 pm #104715TheDoc
MemberThe ‘head’ is
, that’s where you want to append your script to.
June 21, 2012 at 1:53 pm #104716TheDoc
MemberThe 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.
June 21, 2012 at 2:36 pm #104721JohnMotylJr
Participantvar script = document.createElement("script");
script.type = "text/javascript";
script.src = "js/javascript.loading.js";
if ($("#example").length > 0){
document.head.appendChild(script);
}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:
$(function() {
if ( $('div').length > 0 )
{
$.getScript("http://malsup.github.com/jquery.easing.1.3.js", function(data, textStatus, jqxhr) {
console.log(data); //data returned
console.log(textStatus); //success
console.log(jqxhr.status); //200
console.log('Load was performed.');
})
.fail(function(jqxhr, settings, exception) {
$( "div" ).text( "ajax request for script failed." );
});
}
});I feel that the
.fail()
is acting like atry
June 22, 2012 at 1:52 pm #104753 -
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.