Funky pagecreate issue with jQueryMobile - javascript

Here is my simplified code which has two pages which link to each other. The result is the page2 count alert always says there is one #page2 div in the DOM. However pagecreate fires for each time that page2.html has been referenced. First time is 1, second time is 2, and so on...
Can someone explain what is going on and how to get one pagecreate event for page2?
index.html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="jquery.mobile-1.0.1.min.css" />
<script src="jquery-1.6.4.min.js"></script>
<script src="jquery.mobile-1.0.1.min.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="content">
<h3>In Index Page</h3>
Go To Page2
</div>
</div>
</body>
</html>
page2.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="page2" data-role="page">
<div data-role="content">
Back
</div>
<script type="text/javascript">
alert("page2 count is " + $("#page2").length);
$("#page2").live('pagecreate',function(event, ui) {
alert("in page2 on pagecreate");
});
</script>
</div>
</body>
</html>
Thanks,
-- Ed

jQM loads subsequent pages into the same DOM (page) using a debugger/dev tools with Chrome you can easily see this happen.
In subsequent pages it pulls in anything between your tags, but ignores everything else, you could have JS in your or other pages and it'd still only pull in page2.
What exactly is happening with you is that when you load #page2 the first time it properly adds a live event to #page2, then you navigate to index.html, but #page2 remains in your DOM. Now when you goto #page2 again, it will run your JS again
$("#page2").live('pagecreate',function(event, ui) {
alert("in page2 on pagecreate");
});
This binds your alert again, and 2 events fire, next time 3 will fire.
The way you're supposed to do it is load all your JS at the start and listen for the pageinit/pageshow, see my other post: https://stackoverflow.com/a/9085014/737023
Or if you have any questions ask here

Related

Jquery Load Not Loading after going to another page

I have an html page where a JQuery Loads a page into a div on click. I can click on it and it goes to page 2 and it works. I have the same link that goes back to page 1 and it will load the html in page 1, but if I click back to page 2 it will not load. So pretty much I cannot go back and forth on loading a div from Jquery. I have search and seen something on using live() but can't seem to get anything to work. Any Suggestions?
My Jquery Function to load the div from another page.
<script>
function changepage(page) {
$(function(){
$('#maincontent').load('index.php?p='+page);
return false;
});
}
</script>
HTML To load the page 2:
Page 2
HTML To load back to page 2
Page 1
EDIT:
Entire Page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>My Page</title>
</head>
<body>
<div id="wrapper">
<!-- Page Content -->
<div id="page-wrapper">
<div class="container-fluid">
<div id="maincontent">
Page 2
</div>
<!-- /.maincontent -->
</div>
<!-- /.container-fluid -->
</div>
<!-- /#page-wrapper -->
</div>
<!-- /#wrapper -->
<script>
function changepage(page) {
$(function(){
$('#maincontent').load('subpage.php?p='+page);
return false;
});
}
</script>
</body>
</html>
I agree with tokyovariable. you need to link jquery before using it in function. Try adding
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
before script with changepage function.
Also make sure jquery is loaded before changepage function is called. Try
function changepage(page) {
$(document).ready(function(){
$('#maincontent').load('index.php?p='+page);
return false;
});
}
Is jQuery linked to on that page?
I could be wrong, but I don't think you've given us enough information here. The above code looks fine. Is input.php working correctly? What happens when you call:
http://[domain]/index.php?p=page1
http://[domain]/index.php?p=page2
From a browser? Does it output a whole page, or just #maincontent? It's kind of weird, does the index page output a single div, or are you stuffing a whole html page inside another one? Sound like it could be the output from index.php, which would normally be the landing page of your site, so why are you stuffing the whole landing page inside a div?

How to do auto reload in my page

How can I make this possible? I want to auto refresh my page after the history.go. I post my whole html
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="css/styles.css"/>
<link rel="shortcut icon" href="img/favicon.png" type="image/png">
<script src="//cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="https://raw.github.com/andris9/jStorage/master/jstorage.js"></script>
<script>
$.jStorage.set("reload", true);
if ($.jStorage.get("reload") === true) {
$.jStorage.set("reload", false);
window.location.reload()
}
</script>
<script>
function goBack(){
window.history.go(-2);
}
</script>
<title>Edit Success</title>
</head>
<body>
<div id="container">
<div id="content">
<div class="md-modal md-show" id="modal-1">
<div id="notifMessage">
<h1>The information have been updated.</h1>
<p><a onclick='goBack();'>View Patients</a></p>
</div>
</div>
</div>
</div>
</body>
I edited it with my whole html and replaced what u post but still not working or maybe i'm wrong in implementing it
You cant reload after you have gone back like this, because the code that comes after the user navigates away , will never be executed.
Instead, you need to store a variable either on server side, or on user browser, that tells the page to reload. And then...you write a script that reloads if such parameter exists.
e.g By using jStorage plugin you could do something like this
//add jstorage script to your pages, just copy paste this
<script src="//cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="https://raw.github.com/andris9/jStorage/master/jstorage.js"></script>
then before you go back in history, you set the reload variable to true
$.jStorage.set("reload", true);
//Go back in history
and after user has gone back in history, you check if the reload parameter exists, and if so, do a reload.
code below would have to be loaded on every page, so you would put it into the header of your template, after jstorage scripts
if ($.jStorage.get("reload") === true) {
$.jStorage.set("reload", false);
window.location.reload()
}

jQuery .load function gets slow after few clicks

I am trying to load different external HTMl files into a DIV in my parent HTML. Here is the code I am using
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>BeautyDish Photography | Wedding and Beauty Photographer</title>
<meta name="">
<meta name="keywords" content="">
<meta name="viewport" content="width=device-width">
<!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
</head>
<body>
<div id="wrapper" style=" max-width:1920px; min-width:1024px;">
<nav>
<div id='MainMenu'>
Home
About Us
Portfolio
Contact Us
</div>
</nav>
<div id="content" style="width:1200px; padding:0; margin:auto;">ff
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="js/main.js"></script>
<script>
$(function(){
$("#MainMenu a").click(function(e) {
//load home.html on click
e.preventDefault();
$("#content").load($(this).attr('href')+".html", null, function(){
alert('Load Done');
});
});
});
</script>
</body>
</html>
Whenever I click any link for the first time it works perfectly, from second click onward the loading is taking longer and the alert is coming more than once.
I have also hosted a working version at http://www.jbasuphotography.com/index_new.html
I am new to jQuery, please let me know how to solve the problem.
The problem seems to be that you're loading the whole page inside a div of itself.
And it's included with its script and event binding which can only make it worse.
A solution could be to reduce to the part that really interests you (a different page). But this solution would have to be designed for your real need which isn't clear.
before the load you can do a remove first, because its loading multiple files in.
$('#wrapper #content').remove();
$('#wrapper').append('<div id="#content"></div>')

window.onbeforeunload not working in second page

Hi I have two html pages in my mobile application as follows:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery Mobile: Demos and Documentation</title>
<link rel="stylesheet" href="jquery.mobile/jquery.mobile-1.1.0.css" />
<link rel="stylesheet" href="docs/assets/css/jqm-docs.css" />
<link rel="stylesheet" href="docsdemos-style-override.css" />
<script type="text/javascript" src="jquery.mobile/jquery-1.7.2.min"></script>
<script type="text/javascript" src="jquery.mobile/jquery.mobile-1.1.0.js"></script>
<!-- Uncomment following line to access PhoneGap APIs (not necessary to use PhoneGap to package web app) -->
<!-- <script type="text/javascript" charset="utf-8" src="cordova-1.6.1.js"></script>-->
</head>
<body>
<div data-role="page" id="jqm-home" class="type-home">
<div data-role="content">
Index
</div>
</div>
</body>
</html>
my example.html is like:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery Mobile: Demos and Documentation</title>
<link rel="stylesheet" href="jquery.mobile/jquery.mobile-1.1.0.css" />
<link rel="stylesheet" href="docs/assets/css/jqm-docs.css" />
<link rel="stylesheet" href="docsdemos-style-override.css" />
<script type="text/javascript" src="jquery.mobile/jquery-1.7.2.min"></script>
<script type="text/javascript" src="jquery.mobile/jquery.mobile-1.1.0.js"></script>
<!-- Uncomment following line to access PhoneGap APIs (not necessary to use PhoneGap to package web app) -->
<!-- <script type="text/javascript" charset="utf-8" src="cordova-1.6.1.js"></script>-->
</head>
<body>
<div data-role="page" id="jqm-home" class="type-home">
<div data-role="content">
Test
</div>
<script type="text/javascript">
window.onbeforeunload = function(evt) {
console.log ("*****************");
}
</script>
</div>
</body>
</html>
Now suppose on click of Index button I goes to example.html page. on click of back button in example.html it again goes to index.html. everything is fine, but it does not print console.log ("********"); If i press one more back on index.html then it prints it, what I want is it should print that on click of back button when I am on example.html.
Whats wrong in above code? and why it behave like this? Any suggestion will be appreciated thanks in advance.
In Jquery Mobile standard use you basically stay on the same page during your hole experience on the site. "Changing" page basically adds content dynamically to the current document, meaning it will not trigger your onbeforeunload event.
You can however use jquery mobile events, which one depends on exactly what kind of action you want to take. Most probably you would be looking at pagebeforehide or pagehide
What you can do is add an event to the back button.
If you have a button :
<button id="backButton"> Go Back </button>
you can add via jquery following event
$("#backButton).click(function(){
console.log ("*****************");
});
Keep in mind, if you click a button, a post will happen and the page will be refreshed. So you should add the log function inthe document ready of the other page.
So if the example.html page loads, you just fire this event
$(function(){
//All code that starts when the page is fully loaded.
console.log ("*****************");
});

Javascript doesn't rock on generated page

I'm quite new in html5 & js and i have some troubles to develop a jQuery Mobile exemple.
From this, i'm just calling another header (partial) when a product is selected to render.
_header2.php:
<!DOCTYPE html>
<head>
<title>h2: <?php echo $_GET['product']; ?> </title>
<meta charset="UTF-8" />
<meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;' name='viewport' />
<link rel='stylesheet' href='assets/css/styles_mob.css'/>
<link rel='stylesheet' href='assets/css/styles.css' />
<link href='assets/css/jquery-mobile.css?<?php echo filemtime("assets/css/jquery-mobile.css");?>' type='text/css' rel='stylesheet' />
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.min.js'></script>
<script type='text/javascript' src='http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.min.js'></script>
</script>
<script type='text/javascript'>
function mymessage(msg)
{
if(!msg) msg="?";
alert(msg);
}
</script>
</head>
<body onload="mymessage('onload !')">
<div data-role="page" id="Home">
<div data-role="header" data-theme="b">
Home
<h1> <?php echo $title?></h1>
</div>
<div data-role="content">
Header code appair correctly on the generated page but js call of mymessage() in body tag doesn't work. Same problem when i try to call it from another partial code (_product.php) :
<li><a href='#Gallery1' onClick='mymessage(\"press gallery\")' >...</li>
...concole return: referenceError: mymessage is not defined
Everything rock only when a refreshing the current page !!
I've rode some posts (1, 2, 3) with similar problem but i'm still lost.
Any idea please ?
You're missing the <html> tag. You also have an extra </script> closing tag. This could potentially lead to your error.
There are all sorts of strange coding styles and errors like missing the <html> tag, an extra </script> closing tag, missing the closing tag on your anchor in <li><a href='#Gallery1' onClick='mymessage(\"press gallery\")' >...</li>, and list items should be inside <ul></ul>.
Try this code, it should work. Then analyze it and learn!
<!DOCTYPE html>
<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>h2: <?php echo $_GET['product'];?></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;">
<link rel="stylesheet" type="text/css" href="assets/css/styles_mob.css">
<link rel="stylesheet" type="text/css" href="assets/css/styles.css">
<!-- this next line makes me wonder.. why?! -->
<link rel="stylesheet" type="text/css" href="assets/css/jquery-mobile.css?<?php echo filemtime('assets/css/jquery-mobile.css');?>">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.min.js"></script>
<script type="text/javascript">//<![CDATA[
function mymessage(msg){
msg=msg||'?'; //the usual way to provide defaults
alert(msg);
}
window.onload=function(){ //setting your onload event.
mymessage('onload !');
};
//]]>
</script>
</head><body>
<div data-role="page" id="Home">
<div data-role="header" data-theme="b">
Home
<h1><?php echo $title;?></h1>
</div>
<div data-role="content">
<ul> <!-- Here is your link, working -->
<li>...</li>
</ul>
<!-- added some closing tags for demo-sake -->
</div></div>
</body></html>
PS: since all the beginner errors and the fact that this is just part of your code, I strongly assume the rest of your code has such strange error's to.
Good Luck!!
UPDATE:
Depending on what you'd like to accomplish on page-load, jQuery Mobile might completely change the ballpark.
As can be read in jQuery Mobile's documentation:
By default all navigation within jQuery Mobile is based on changes and
updates to location.hash. Whenever possible, page changes will use a
smooth transition between the current "page" and the next, whether it
is either already present in the DOM, or is automatically loaded via
Ajax.
Another quote from the documentation:
Use $(document).bind('pageinit'), not $(document).ready()
The first thing you learn in jQuery is to call code inside the
$(document).ready() function so everything will execute as soon as the
DOM is loaded. However, in jQuery Mobile, Ajax is used to load the
contents of each page into the DOM as you navigate, and the DOM ready
handler only executes for the first page. To execute code whenever a
new page is loaded and created, you can bind to the pageinit event.
This means that the regular techniques described will only fire when you first visit the page, not while navigating the page ajax-style.
Thus in jQuery mobile you must use pageinit or pageshow or one of the other events that are explained in the documentation that suit your exact purpose.
Example how to get pageinit fire for every page (tested live on your site):
$(document).on('pageinit','[data-role=page]', function(){
mymessage('hihi');
});
Or alternatively you could turn off ajax in jQuery Mobile, like this (depends on version):
$(document).bind("mobileinit", function(){
$.mobile.ajaxEnabled = false;
});

Categories