Loading Sequence of JQuery objects in Firefox different than in Chrome/Opera? - javascript

I have problems with the loading sequence of jQuery objects in a HTML and Javascript file when executed on Firefox.
I have several <div> and <button> tags with different id. The page starts with following code:
<script>
$(document).ready(function () {
$("#id_1").show();
$("#id_2").hide();
$("#id_3").hide();
...
});
$(document).ready(function () {
$.getJSON("JSON_file.json", function (json) {
if (json.option[number] == "b") {
$("#id_2").show()
};
if (json.option[number] == "c") {
$("#id_3").show()
};
});
});
</script>
In Chrome and Opera I have no issues with this code. The objects "#id_2" and "#id_3" are not visible on load.
However in Firefox both "#id_2" and "#id_3" are visible for a short moment before being hidden. But I don´t want them to be visible at the begin.
I use localhost to open the files.
Does anybody know what I´m doing wrong?

Hello You can add default hide call in div and then remove that call on condition basic.
Like
<div id="#id_1" class='hide'>
My Div 1
</div>
<div id="#id_2" class='hide'>
My Div 1
</div>
<div id="#id_3" class='hide'>
My Div 1
</div>
Now your script code
<script>
$(document).ready(function () {
$.getJSON("JSON_file.json", function (json) {
if (json.option[number] == "b") {
$("#id_2").removeClass('hide')
};
if (json.option[number] == "c") {
$("#id_3").removeClass('hide')
};
});
});
</script>

Related

Ignored attempt to cancel a touchstart : fastclick warning

I'm having the first popup on which another popup comes to select few fields.
To show the second popup this is the code I'm trying:
$("#select1").click(function(e) {
e.stopPropagation();
//$('#sellerModal').hide();
var tmplData = {
string:['Ready','2-3 Days','4-5 Days','1 Week','10+ Days']
};
$("#countTypePopupTemplate").tmpl(tmplData).appendTo(".maindiv");
that.closePopup();
$("#count_div").each(function() {
$("#count_div").click(function(evt) {
evt.stopPropagation();
$("#select1").text($(this).text());
$("#myCounttype").remove();
});
});
});
Here is the HTML template:
<script id="countTypePopupTemplate" type="text/x-jquery-tmpl">
<div id="myCounttype" class="popup1 layer-2">
<div class="popup5">
{{each string}}
<div id="count_div" class="popup4 bottom-border">${$value}</div>
{{/each}}
</div>
</div>
</script>
I'm getting a warning:
Ignored attempt to cancel a touchstart event with cancelable=false, for example, because scrolling is in progress and cannot be interrupted. fastclick.js
Here I'm not able to click the 4 out of 5 elements in the second popup. Only first 1 is clickable.
Snapshot of second popup.
I read all the blogs where the topic is disscussed. But didn't got any solution working for me. Looks like there is some corner case.
Try using some parent selector before $("#count_div").each(function() {
$("#count_div").click(function(evt) {
Like this $(".parent_class #count_div").each(function() {
$(".parent_class #count_div").click(function(evt) {
This will solve 1 time running each() for "#count_div".
So the actual problem is each() is running only 1 time, that's why your first element that is Ready click event is working, not others.
Your each function is pointing to id which makes you cannot click other buttons. You should use class to recognize the buttons.
$("#select1").click(function(e) {
e.stopPropagation();
//$('#sellerModal').hide();
var tmplData = {
string:['Ready','2-3 Days','4-5 Days','1 Week','10+ Days']
};
$("#countTypePopupTemplate").tmpl(tmplData).appendTo(".maindiv");
that.closePopup();
$(".pop_btns").each(function() {
$(this).click(function(evt) {
evt.stopPropagation();
$("#select1").text($(this).text());
$("#myCounttype").remove();
});
});
});
HTML template:
<script id="countTypePopupTemplate" type="text/x-jquery-tmpl">
<div id="myCounttype" class="popup1 layer-2">
<div class="popup5">
{{each string}}
<div id="count_div" class="popup4 bottom-border pop_btns">${$value}</div>
{{/each}}
</div>
</div>
</script>

Focus a tab by Javascript issue

I have a HTML page that have 2 tabs settings1, settings2. I want to focus the tabs according to tab_id from javascript. My code is below;
HTML
<form action="action_here" method="post" enctype="multipart/form-data" name="form11">
<ul class="tab">
<li>settings1</li>
<li>settings2</li>
</ul>
<input type="text" id="tab_id1" name="tab_id" >
</form>
Javascript
var tab_id = document.getElementById("tab_id1").value;
if(tab_id == 1){
alert("1");
document.getElementById('selected_tab1').focus();
} else if (tab_id == 2) {
alert("2");
document.getElementById('selected_tab2').focus();
}
Issues:
when page first loaded no tabs are selected. I want to select tab 1 settings1.
2.When I select tab, tab won't selected, but tab_ids are printing right by javascript.
Plaese Help me with this.
You want to make sure your code gets called after your DOM content is ready. If you don't want to use something like jQuery you can do something like this:
function ready(fn) {
if (document.readyState != 'loading'){
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
}
ready(function() {
// your code
})
The implementation of document ready was taken from you might not need jquery.

window.load document read does not run

I have a simple html5 page that renders a table with several rows. On two of the rows I gave a button that I want to act as a toggle button to show/hide a couple of other rows each.
The table is rendered intitally will all rows then I have a function that hides the 4 columns and sets the buttons to have the correct symbol.
When I run the page all the rows are visible... the window.load function does not run. I even tried document.ready and no joy. The toggle buttons once the inital click hides the rows functions correctly.
My code is...
<script type="text/javascript">
function toggleRetail() {
if ($('#but_RToggle').val()=="+"){
$('#but_RToggle').val("-");
$('#qretail').show("slow");
$('#hretail').show("slow");
} else {
$('#but_RToggle').val("+");
$('#qretail').hide("fast");
$('#hretail').hide("fast");
}
}
function toggleWholesale() {
if ($('#but_WToggle').val() == "+") {
$('#but_WToggle').val("-");
$('#qwholesale').show("slow");
$('#hwholesale').show("slow");
} else {
$('#but_WToggle').val("+");
$('#qwholesale').hide("fast");
$('#hwholesale').hide("fast");
}
}
$(window).bind("load", function () {
$('#but_RToggle').val("+");
$('#qretail').hide();
$('#hretail').hide();
$('#but_WToggle').val("+");
$('#qwholesale').hide();
$('#hwholesale').hide();
});
</script>
Any help appreciated.
Make sure that the jquery script tag is before this script block.
For loading, any of these:
$(document).ready(function(){
//code
});
$(function(){
//code
});
window.addEventListener('load', function(){
//code
},false);

ReferenceError in js console -SCRIPT5009: 'channel_click' is undefined

I'm getting a "SCRIPT5009: 'channel_click' is undefined" error in IE9 within the js console. The code works in chrome & firefox but it suddenly stopped while clicking the link to initiate the js sequences in IE9.
I have been trying to figure this out with no success but what I have done was timed a series of events to occur which:
1) onclick - toggle div to hide
2) wait 1500
3) open print command
- User closes the window to return to page
4) wait 3500 toggle div to show again
The reason why I do this is I don't want the print preview to pick up these series of divs when the user decides to print the page.
Javacript:
<script>
//WAITS UNTIL EXCESS IS HIDDEN THEN FIRES PRINT COMMAND
function channel_click(){
// anon wrapper function, 2 second delay
setTimeout( function () {
window.print();return false;
} , 1500 );
}
</script>
<script>
//HIDES EXCESS IN PREP FOR FIRING PRINT COMMAND PREVIOUS FUNCTION EXECUTES IN BETWEEN FOLLOWING FUNCTIONS
$(".hideshow").click(function () {
$("#header,#footer").toggle("slow");
setTimeout( function () {
$("#header,#footer").toggle("slow");
} , 3500 );
$('.modal-backdrop').hide();
});
</script>
HTML:
Print Preview
<div id="header">
<div class="pull-left">
<h1>Edt Mode</h1>
<h6 style="padding-bottom: 30px;">Some informational text here</h6>
</div>
<div>
<div class="pull-left">
<h1>PRINT ME!</h1>
<h6 style="padding-bottom: 30px;">PRINT ME - PRINT ME - PRINT ME</h6>
</div>
<div id="footer">
<div class="pull-left">
<h1>Edt Mode</h1>
<h6 style="padding-bottom: 30px;">Some informational text here</h6>
</div>
<div class="model-backdrop">wow this is some more text</div>
Do you load you javascript file before you try to use it in the html?
If you do that that's not the problem but if you don't try to link to the .js file at the end of your html file.
If you don't want this to be nessecary you have to wrap your channel_click within a window.onload or if you use jQuery
$(document).ready(function() {
function channel_click() {
....
}
})
try removing onclick from your anchor tag:
Print Preview
JS:
<script type="text/javascript">
function channel_click(){
// anon wrapper function, 2 second delay
setTimeout( function () {
window.print();return false;
} , 1500 );
}
$(document).ready(function() { //add this
$(".hideshow").click(function (e) {
e.preventDefault();
$("#header,#footer").toggle("slow");
channel_click(); //call print
setTimeout( function () {
$("#header,#footer").toggle("slow");
} , 3500 );
$('.modal-backdrop').hide();
});
});
here it is working as u expected
http://jsbin.com/imimom/2
Out of that your html is not well-formed, I don't see something wrong there. Maybe you can provide us with the complete markup?

Changing jQuery function to page load

Can anyone tell me how to change the following statement which is currently controlled by a radio button to a document load function.
Basically I want the slide div to slide out from the left to right when the page loads.
jQuery:
jQuery(function() {
jQuery('#slide').hide();
jQuery(':radio[name=radiobutton]').click(function() {
var value = $(this).val();
if (value === 'Y') {
jQuery('#slide').show('slow');
} else if (value === 'N') {
jQuery('#slide').hide('slow');
}
});
HTML:
<div id="slide" class="alpha60">
<p class="statement_style_head">Content goes here</p>
<p class="statement_style_text">A line or two of text goes here </p>
</div>
$(document).ready({. Your staff. });
One way is to just click it on load like so $('input[name=radiobutton]').click(); or $('input[name=radiobutton]').trigger('click');
The other would be to place your code inside a function and call that function on load.
function showSomething(){
// code here
}
showSomething();
Document.ready is kind of onload for javascript , it waits till everything is ready and then executes...your code , it safe and best practices for onload
$(document).ready(function() {
$("divid").animate({top:'10px', bottom:0}, 3000);
});

Categories