I'm having some trouble getting my jquery to render correctly 100% of the time - the code I am using is located below. Its purpose is to "simulate" the feel of a threaded forum by hiding everything but the subject of replies - when a subject is clicked, the 1st post is then replaced with the reply.
You can see an example of it in action here:
http://bulldogsworld.com/general-bulldog-chat/50-lbs-bulldog-one-shin-pic
The problem is the script doesn't work so well when people land via a bookmark # in the URL, such as:
http://bulldogsworld.com/general-bulldog-chat/50-lbs-bulldog-one-shin-pic#comment-1627028
Specifically, the problem which happens is for some reason all posts below the bookmark entry point are replicated twice. I can't figure out why this is happening - any thoughts?
I'm pulling my hair out on this one - Any help / guidance is greatly appreciated!
function flip(comment) {
$('#first-post').replaceWith(comment.closest(".comment").clone().attr('id','first-post'));
$('#first-post').children('.forumthreadtitle').children('.comment-info').empty();
$('#first-post').find(':hidden').fadeIn('slow');
$('html, body').animate({scrollTop:0}, 'fast');
return false;
}
$(document).ready(
function(){
$('.submitted').each(function() {
$(this).clone().addClass('comment-info').appendTo($(this).siblings('.forumthreadtitle'));
if(!$(this).parent('#first-post').html()) {
$('#first-post').children('span.taxonomy').clone().appendTo($(this));
}
});
$('.display_mode').html('Show All Replies');
expandedMode = false;
$('.display_mode').click(function() {
if ( expandedMode == false ) {
$('.forumthreadtitle').siblings().show();
$(this).html('Collapse Replies');
expandedMode = true;
}
else
{
$('.forumthreadtitle').siblings().hide();
$(this).html('Show All Replies');
expandedMode = false;
}
});
$('.forumthreadtitle').siblings().hide();
if(window.location.hash) {
flip($(window.location.hash).nextAll().children('.forumthreadtitle').show());
}
$('.forumthreadtitle').click(function() {
pageTracker._trackPageview("/comment?page=" + document.location.pathname);
flip($(this));
} );
});
You probably need to use next() instead of nextAll() in your flip
flip($(window.location.hash).next().children('.forumthreadtitle').show());
nextAll() returns all elements after selected, and they all are passed to flip function.
Related
I have a form with different tabs. One of the tabs requires authorization to be set so that only certain users can see it.
The problem is this authorization breaks the Javascript for any users that are restricted from that form tab - it works fine if you have authorization. The reason is that there are checkboxes/textfields in that tab that the code refers to, so the code breaks. It can't find these objects.
I need a way of somehow ignoring those checkboxes/textfields so the code doesn't break.
The below events call a function called CommStat. If, for example $("#ADD_CH") lived on a form tab that had authorization set, it wouldn't exist on run time, so the code breaks.
$("#STATUS").change(function() {
CommStat('STATUS', 'STATUS_COMMENT');
});
$("#DISPOSED").change(function() {
CommStat('DISPOSED', 'VER_COMM');
});
$("#ADD_CH").change(function() {
CommStat('ADD_CH', 'ADDRESS3');
});
$("#U_STAND").change(function() {
CommStat('U_STAND', 'IN_COMM');
});
$("#M_INAP").change(function() {
CommStat('M_INAP', 'IN_COMM');
});
$("#U_INAP").change(function() {
CommStat('U_INAP', 'IN_COMM');
});
$("#CON_RATE").change(function() {
CommStat('CON_RATE', 'IN_COMM');
});
$("#BEYOND").change(function() {
CommStat('BEYOND', 'IN_COMM');
});
$("#OUT_INT").change(function() {
CommStat('OUT_INT', 'IN_COMM');
});
Is there an easy way to ignore any objects that don't exist?
I've spent a great deal of time trying to get this working, looking through the forums etc. but without any luck.
One suggestion was to check if the object exists before attaching the event...
e.g.
if(IG.form.getItemById('ADD_CH') != null)
{
IG.form.getItemById('ADD_CH').value.subscribe(CommStat);
}
No luck though.
Hope that makes sense,
you can check using jQuery
if ($('#myElement').length > 0) {
// it exists
}
var myFields = {
"STATUS":"STATUS_COMMENT",
"DISPOSED", "VER_COMM",
...
"OUT_INT", "IN_COMM"
}
$.each(myFields,function(fName, func) {
var $field=$("#"+fName);
if ($field.length) $field.on("change",function(){
CommStat(fName,func);
});
});
which is more elegant, but which does not explain the breakage
Thanks for the replies. I figured it out this morning with the following. A simple try and catch ignores the text field if it doesn't exist.
CommStat('DISPOSED', 'VER_COMM');
CommStat('STATUS', 'STATUS_COMMENT');
CommStat('ADD_CH', 'ADDRESS3');
try { // JG300714
CommStat('U_STAND', 'IN_COMM');
CommStat('M_INAP', 'IN_COMM');
CommStat('U_INAP', 'IN_COMM');
CommStat('CON_RATE', 'IN_COMM');
CommStat('BEYOND', 'IN_COMM');
CommStat('OUT_INT', 'IN_COMM');
}
catch(e){// Ignore any error // JG300714
}
This should be quite simple but I'll be darned if I can work it out. Just trying to get a div to display while my ajax is processing and then hide once done (I've put a sleep in there purely to test its working as locally it loads so fast I'm not sure if its working or not)!
The html page has this code in the script: -
$(document).ready(function(){
$("#loadingGIF").ajaxStart(function () {
$(this).show();
});
$("#loadingGIF").ajaxStop(function () {
window.setTimeout(partB,5000)
$(this).hide();
});
function partB(){
//just because
}
var scenarioID = ${testScenarioInstance.id}
var myData = ${results as JSON}
populateFormData(myData, scenarioID);
});
There is then a div in my page like so (which I can see in the source of the page just hidden): -
<div id="loadingGIF" ><img src='${application.contextPath}/images/spinner.gif' height="50" width="50"></div>
The ready code then goes off and calls this: -
function populateFormData(results, scenarioID) {
$table = $('#formList')
for(var i in results){
var formIDX = (results[i]["forms_idx"])
var formID = (results[i]["form_id"])
appendSubTable(formIDX, scenarioID, $table, formID);
}
}
Which references this multiple times calling several AJAX posts: -
function appendSubTable(formIDX, scenarioID, $table, formID) {
var $subTable = $table.find("#" + formIDX).find('td:eq(1)').find("div").find("table")
var url = "**Trust me this bits OK ;) **"
$.post(url, {
formIDX : formIDX, scenarioID : scenarioID, formID :formID
}, function(data) {
$subTable.append(data)
}).fail(function() {
});
}
Any pointers gratefully received...
Interestingly I bunged some alerts into my ajaxstart and stop and neither show up ever so I'm missing something obvious :S When I check the console in firefox I can see that all my POSTs are completing....
You should probably add the Ajaxstart and stop global event handlers to the document node like this
$(document).ajaxStart(function () {
$("#loadingGIF").show();
});
I realized my problem, I needed to register the ajaxstart and stop to the document not the div!
So instead of this: -
$("#loadingGIF").ajaxStart(function () {
$(this).show();
});
I now have: -
$(document).ajaxStart(function () {
$("#loadingGIF").show();
});
I assume this is because its the document that the ajax is running against not the div although my understanding there may not be 100% accurate at least this works so please tell me if I've misunderstood this! :)
#jbl, thanks for this pointer I did this to also leave the notification on screen for a few more moments just to make sure everything is loaded.
I'm really new to jQuery but familiar with some other languages. I recently bought a quiz type script and I'm trying to add a simple 15 second timer to each question. It's only a fun quiz, so no need to worry about users playing with the javascript to increase time etc.
Basically, if a user does not pick a question within 15 seconds, it will automatically go on to the next question and the timer starts over again.
Answers have the .next tag, and when chosen it moves onto the next question as the code below shows (hopefully).
superContainer.find('.next').click(function () {
$(this).parents('.slide-container').fadeOut(500, function () {
$(this).next().fadeIn(500)
});
return false
});
The problem i have is if i use setInterval, i don't know how i can select the appropriate div again for fade it our and fade in the next one. I've tried the below code and a few similar scrappy idea's but it doesn't work, but maybe it will give a better idea of what I'm after though.
superContainer.find('.next').click(function () {
$active_count = $count;
countInterval = setInterval(function() {
$active_count--;
if($active_count <= 0){
clearInterval(countInterval);
$active_count = $count;
$(this).parents('.slide-container').fadeOut(500, function () {
$(this).next().fadeIn(500)
});
}
$('.question-timer').html($active_count);
}, 1000);
$(this).parents('.slide-container').fadeOut(500, function () {
$(this).next().fadeIn(500)
});
return false
});
I've only been using JQuery a day or two so excuse any obvious mistakes and bad code! Let me know if you need any other code or information
This is moderately tricky for a first jQuery project.
The knack (in this solution) is to factor out a goNext function that can be called in two ways - in response to a click event and in response to a 15 second setTimeout(), not setInterval().
$(function(){
var questionTimeout = null;
function goNext($el) {
clearTimeout(questionTimeout);
var $next = $el.next();
$el.fadeOut(500, function() {
if($next.length > 0) {
$next.fadeIn(500, function() {
questionTimeout = setTimeout(function() {
goNext($next);
}, 15000);
});
}
else {
afterLastQuestion();
}
});
}
function afterLastQuestion(){
alert("last question complete");
$start.show();
}
var $superContainer = $("#superContainer").on('click', '.next', function() {
goNext($(this).closest('.slide-container'));
return false;
});
var $start = $("#start").on('click', function(){
$(this).hide();
$superContainer.find(".slide-container")
.eq(0).clone(true,true)
.prependTo(superContainer)
.find(".next").trigger('click');
return false;
});
});
DEMO
The process is started by clicking a "start" link, causing the first question to be cloned followed by a simulated click on the clone's "next" link. This ensures that the (actual) first question is treated in exactly the same way as all the others.
I also included a afterLastQuestion() function. Modify its action to do whatever is necessary after the last question is answered (or times out).
You could keep the current question in a variable, resetting it on a next click and in the timer, e.g.
var $current;
superContainer.find('.next').click(function (e) {
e.preventDefault();
$(this).parents('.slide-container').fadeOut(500, function () {
$(this).next().fadeIn(500);
$current = $(this).next();
});
});
You'll just need to set it to your first question on initialisation, and remember to reset your timer on a next click
Also, it's usually preferable to use e.preventDefault() rather than return false.
I'm trying to set the current page to reflect in a main navigation bar. I've set a div in each page for JS / jQuery to check it's the right page with the following code:
$(document).ready(function() {
if ($('div').is('#example1-page')) {
$("a#nav-example1").addClass('nav-example1-current');
$("#nav-example1 p").css('display','block');
}
else if ($('div').is('#example2-page')) {
$("a#nav-example2").addClass('nav-example2-current');
$("#nav-example2 p").css('display','block');
}
else if ($('div').is('#example3-page')) {
$("a#example3").addClass('nav-example3-current');
$("#nav-example3 p").css('display','block');
}
});
I know there has to be a better way to do this with an array or something where I'm not repeating myself. Something where you can store the links IDs in an array and by clicking on the link with the right ID JS will know to set the current page in the navigation. I'm not sure where to start though.
Arrays of arrays are your friend :). Try this:
$(document).ready(function() {
var pageCounter=3;
var pages=[];
for(var i=1;i<=pageCounter;i++){
var selectors=["#example"+i+"-page",
"a#nav-example"+i,
"nav-example"+i+"-current",
"#nav-example"+i+" p"];
pages.push(selectors);
}
for(var i=0;i<=pageCounter;i++){
if ($('div').is(pages[i][0])) {
$(pages[i][1]).addClass(pages[i][2]);
$(pages[i][3]).css('display','block');
break;
}
}
});
Note: the logic to initialize the pages array does not need to be part of document.ready.
Try this
$(document).ready(function() {
if ($('#example1-page').length) {
$("a#nav-example1").addClass('nav-example1-current');
$("#nav-example1 p").show();
}
else if ($(#example2-page').length) {
$("a#nav-example2").addClass('nav-example2-current');
$("#nav-example2 p").show();
}
else if ($('#example3-page').length) {
$("a#example3").addClass('nav-example3-current');
$("#nav-example3 p").show();
}
});
Problem solved: I removed the first function as it wasn't needed anyways, and now it all works in all browsers. Thanks for the posts though!
My issue here is that I have a javascript which on one server runs perfectly with no issues what-so-ever in IE. But on another server gives me 2 errors in IE.
It claims that my offset().top is either Null or not an object.
My fade in and out effect doesn't even become active.
Then I have another HUGE issue, which is that in FireFox NON of it all works.
OPS: This is a webpart, so additional javascripts running on the site might could intervene with the javascript I am trying to execute here. But I'm not sure.
I've tested this webpart in IE 8.0.7600.16385, on both servers.
Script:
<script type="text/javascript"
src="/_layouts/Progressive/Javascripts/jquery-1.4.3.js"></script>
<script type="text/javascript">
(function($) {
$.fn.goTo = function() {
// This is where IE on the second server claims the error to be.
$('html, body').animate({scrollTop: $(this).offset().top + 'px'}, 'fast');
return this;
}
})(jQuery);
function showParagraphs(sender) {
var id = sender.getAttribute('href');
if ($('#<%=paragraph.ClientID%>').hasClass("readable")) {
$('#<%=paragraph.ClientID%>').removeClass("readable");
highlightSelected(id);
}
else {
$('#<%=paragraph.ClientID%>').addClass("readable");
rmvClass(id);
}
};
function highlightSelected(id) {
$(id).goTo();
$(id).addClass("reading");
// This part is what isn't activated on the second server.
$('.reading').fadeOut(400).fadeIn(400).fadeOut(400).fadeIn(400);
// .reading only adds a gray background to the DIV.
};
function rmvClass(id) {
$('div').each(function() {
if ($(this).hasClass("reading")) {
$(this).removeClass("reading");
}
});
}
function toTop() {
$('div').each(function() {
$(this).removeClass("reading");
});
$('#<%=paragraph.ClientID%>').addClass("readable");
}
$(document).ready(function() {
$('#<%=q.ClientID%>').find('dd').hide().end().find('dt').click(function() {
$(this).next().slideToggle("fast");
});
$("#<%=q.ClientID%> dt").click(function() {
if ($(this).hasClass("selected")) {
$(this).removeClass("selected");
}
else {
$(this).addClass("selected");
}
});
});
</script>
Any thoughts or suggestions?
When
$(id)
returns an empty jQuery object, then ".offset()" will return null. You're calling that "highlightSelected" code with the "href" value from something, so perhaps what you think to be an "id" value that corresponds to something on the page, well it really doesn't. It's hard to know for sure, however, because you did not post any of the relevant HTML, nor did you even show where "showParagraphs()" is called!