up to now, I still haven't figured out why the $.mobile.changePage won't fire in next/second page, it only works in the index page. I get it that using jquery mobile all script will be only loaded once because jqm is built in ajax way. But I still can't find a solution to my problem.
This script below don't fire if not in the index.page or first load.
$(document).bind("pageinit", function(){
$('.sns-down').bind('swipe', function(event) {
$.mobile.changePage('#featured', { transition: "slidedown"});
});
});
I made a simple demo and I attached it here for more clarification.
Download Link
Please help.
Don't use $(document).bind, use the new on delegation, this places a listener at your document root which listens to pageinit/pageshow events that bubble up when new pages are added by jQM.
$(document).on('pageinit', 'div:jqmData(role="page"), div:jqmData(role="dialog")', function(event){
$(this).find('.sns-down').bind('swipe', function(event) {
$.mobile.changePage('#featured', { transition: "slidedown"});
});
Don't use a straight $('.sns-down') this selector may inadvertently select elements not within your current page, you should always search from the current page.
Next thing to check is if #featured exists, if you included it in your first page then it'll be there. If your second page is a multi-page template and you link to it, jQM only pulls in the single page from your multi-page template. Just try an actual page to rule this issue out.
Use a debugger/alert to ensure that pageinit is caught properly by the page transition to rule that out, but it should work.
Hiya probably this might give you an idea you can copy paste the code:
http://jsfiddle.net/r4DyU/1/
Can you jsfiddle yours but above should help.
cheers!
Related
I want to change the content of a div (#mydiv) via AJAX.
To do that I create a button (#change-button) inside #mydiv that trigger the following jQuery script
$(#change-button).on("click", function(){
$("#mydiv").load("page_to_be_loaded.hmtml #mydiv");
});
Basically I replace the text in #mydiv with the new page's one.
This code works fine for the first time, but if I click again on the freshly loaded #change-button jQuery doesn't fire anymore.
I understand the theory: the new #mydiv that has been injected by .load() is not seen by the DOM, so the jQuery script doesn't work.
I found at least two similar questions on SO, but none was properly answered and overall none seems to solve my problem. Can anyone explaining clearly the best practice in these cases.
Here is the other questions:
Update DOM after insert in jQuery
.on()-Function does not rebuild DOM
Thank you
Use delegation like this:
$("body").on("click","#change-button", function(){
$("#mydiv").load("page_to_be_loaded.hmtml #mydiv");
});
I think problem is because y our contents are dynamically loaded, so they are not present at the time of event binding. That's why it's not firing.
You can use delegation like bellow
$(document).on("click",'#change-button',function(){})
I'm pretty sure this was somewhere discussed, but I cant seem to find it anywhere. I would really appreciate if anyone could point me to the right direction.
I have setup a dialog which opens and closes just fine. I need to load dynamic content in it and so Im using this:
$('#dialog').load("somepage.php?document=1");
This load up correctly and everything works pretty much fine except the fact that once I close the dialog and then open it with some different query string (e.g. with document=2) I can see that there are still contents of document=1 loaded in DOM.
This causes issue when there is javascript function in the Loaded page because than it gets executed twice. (well the number of times I load documents so its pretty much unusable).
I have tried clearing the dialog:
$('#dialog').html("")
But that didnt help much.
Does anyone have any idea what could help?
Instead of just closing the dialog, destroy it:
$('#dialog').dialog("destroy");
That should clear the dialog back to its initial state:
https://api.jqueryui.com/dialog/#method-destroy
If you want to just load part of a page, you can always do the following: Let's say I want a specific div only.
$('#dialog').load("somepage.php?document=1 #myDiv");
Furthermore, I don't know what you're closing your #dialog with, but if it was something with an id of myButton, you'd do:
$(document).on("click", "#myButton", function(e) {
// To keep div within DOM and empty
$("#dialog").empty();
// To get rid of div (you would have to re-append #dialog every time)
$("#dialog").remove();
});
All this being said, .load() doesn't actually execute any scripts from your loaded page. If you were to use $.post() or $.get(), you could strip out the <script> tags yourself.
Best of luck!
I'm having a jQuery mobile page with JavaScript inside. The problem is the JavaScript doesn't work unless the page is refreshed. Here is my code:
jQuery(function($) {
var url = window.location.search.substring(1);
$('#mydiv').load('real_news.asp?' + url);
});
To understand this problem you need to understand how jQuery Mobile works.
Your first problem is point where you are trying to initialize JavaScript. From your previous answers I can see you are using several HTML/ASP pages and all of your javascript is initialized form the page <head>. This is the main problem. Only the first HTML file should have JavaScript placed into the <head> content. When jQuery Mobile loads other pages into the DOM it loads only the <div> with a data-role="page" attribute. Everything else, including <head>, will be discarded.
This is because currently loaded page has a <head> already. No point in loading another pages <head> content. This goes even further. If you have several pages in a second HTML file, only the first one is going to be loaded.
I will not try to invent warm water here so here are links to my other 2 answers discussing this problem. Several solutions can be found there:
Why I have to put all the script to index.html in jquery mobile (or in this blog article)
Link fails to work unless refreshing
There's more then enough information there to give you an idea what to do.
The basic solutions to this problem are:
Put all of your JavaScript into a first HTML/ASP file
Move your JavaScript into <body>; to be more precise, move it into a <div> with data-role="page". As I already pointed out, this is the only part of a page that is going to be loaded.
Use rel="external" when switching between pages because it will trigger a full page refresh. Basically, you jQuery mobile that the page will act as a normal web application.
As Archer pointed out, you should use page events to initialize your code. But let me tell you more about this problem. Unlike classic normal web pages, when working with jQuery Mobile, document ready will usually trigger before page is fully loaded/enhanced inside the DOM.
That is why page events were created. There are several of them, but if you want your code to execute only once (like in case of document ready) you should use the pageinit event. In any other case use pagebeforeshow or pageshow.
If you want to find out more about page events and why they should be used instead of document ready take a look at this article on my personal blog. Or find it here.
Your question isn't exactly overflowing with pointers and tips, so I'm going with the thing that immediately sprung to mind when I saw it.
Document ready does not fire on page change with jQuery Mobile, due to "hijax", their method of "ajaxifying" all the links. Try this instead...
$(document).on("pageshow", function() {
var url = window.location.search.substring(1);
$('#mydiv').load('real_news.asp?' + url);
});
Try pageinit like this
$(document).delegate("body", "pageinit", function() { // Use body or page wrapper id / class
var url = window.location.search.substring(1);
$('#mydiv').load('real_news.asp?' + url);
});
seems like nothing ever worked for me. Tried many different fixes, but i made the site too messy, that even position of certain javascript files wouldn't make the site work. Enough talk, here is what i came up with.
// write it in head at top of all javascripts
<script type="text/javascript">
$(document).ready(function() {
// stops ajax load thereby refreshing page
$("a,button,form").attr('data-ajax', 'false');
// encourages ajax load, hinders refresh page (in case if you want popup or dialogs to work.)
$("a[data-rel],a[data-dialog],a[data-transition]").attr('data-ajax', 'true');
});
</script>
I'm experiencing an issue (which I still need to fix) where my entire page does not load. It gets cut off in the middle of an element.
Is there a way using jquery or javscript to do something like:
if (some element has not loaded yet) {refresh the page}
I was thinking of just putting <div id="end_of_page"></div> at the bottom of the page, and checking if that was rendered, if not, i'll know something went wrong and can reload to try again.
thanks!!
Is there a way using jquery or javscript to do something like:
if (some element has not loaded yet) {refresh the page}
You can do that, yes. Put this in the head after including jQuery:
<script>
jQuery(function($) {
if (!$("#end_of_page")[0]) {
// Something went wrong, load again
location.reload();
}
});
</script>
You'll enter the body of the loop (and so, force a reload) if the end_of_page element doesn't exist as of when the jQuery ready event fires (which is meant to be when the page is done loading).
But: Better by far to figure out why your page is getting cut off half-way through and solve that. This sort of workaround is not a solution.
You can use
$(document).ready(function(){
// code here
});
that code will only run when the page has loaded. A convenient shorthand is:
$(function(){
// code here
});
assuming your jQuery object is $. To finish your requirement, you can have a variable that is set within the load function, then use a timer set at the start of the page to check for it. If it's not there, reload.
Personally, I think you should invest some time into figuring out why your pages only half-way (Firebug or the Chrome Inspector may help you do this, it might be a resource in your page that is causing it to hang, and since most HTTP requests are only made 2 at a time per hostname, it might be waiting for that to return before fetching the rest).
Might be a server-side issue OR some script or library is stealing your fish $ (AKA: 'Dollar').
I'll rather suggest you to debug your code instead of refreshing the page trying to fix issues.
Make sure your scripts are in the head of your document, and jQuery + your jQ functions right before the closing </body> tag wrapped in:
(function($){ /*your functions*/ })(jQuery);
I'm a jQuery newb. I'm building a Online Shop and I'm using jQuery plugin 'Thickbox' (http://jquery.com/demo/thickbox). The Online Shop is set up to display a 'Enlarged Image' which runs the Thickbox JS script and shows an image.
A problem occurs as the data is placed on the page thru a CMS system (using Ajax I believe?). When you choose another product from the 'Choose Type' select drop down new data from the CMS system is placed on the page and the Thickbox script isn't applied as it can't see the new data.
I've been playing around with a fix I've found (http://www.codynolden.com/blog/2009/01/thickbox-and-ajax-using-livequery) but I can't seem to apply it to my website?
A live example of my site: http://madisonlane.businesscatalyst.com/_product_36331/AAA-_Autumn_Dress
Any ideas?
in thickbox.js:
$(domChunk).click(function(){
was replaced with this:
$(domChunk).live(click, function(){
Stuart got it right: thank you so much!
I was stuck with a similar problem issue: in my case, the target of an appended link (through jQuery .append() method ) was not loaded in a thickbox.
My jQuery code was executed in a document.ready() function:
$("#subpages").append('','<a href="..." class="thickbox" [...]');
Simply adding:
tb_init('.thickbox'); //required to reinitialize thickbox as DOM was manipulated
just after the last line fixed the problem.
Are you using an IFRAMED thickbox? You may have better luck with that, since by default thickbox is just displayed in a DIV. You need to add &TB_iframe=true to your URL's querystring to get this
I may not be reading your question right, but from the way it sounds, content is being destroyed and created with new item selections, right?
If that's the case, you'll need to call tb_init('.some-selector') when you load new content. I recall having to do this for conditionally setting up a thickbox once. Thickbox works by going through your page and wiring up click events to link with class "thickbox." By using tb_init(), you can wire your stuff up at any time on any selector you like. Check out the source code for Thickbox to see what I mean.