0

I have a site made up of various html pages in jQuery mobile. On one page I have a javascript function in the content. Upon going to another page, this function still exists. How can I remove it before displaying the next page?

I am using the following, which removes the dom elements on the previous page, but the javascript functions from the previous page are still available.

$('div').live('pageshow',function(event, ui) {
    $(ui.prevPage).remove();
});
$('div').live('pagehide', function(event) {
    $(event.target).remove();
});

Here's the full code of two pages. Upon clicking from page 1 to page 2, the function testContent which is only on page 1 still works.

Page 1

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Page 1</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" />
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
<script>
$('div').live('pageshow',function(event, ui) {
    $(ui.prevPage).remove();
    doPageShow();
});
$('div').live('pagehide', function(event) {
    $(event.target).remove();
});
</script>
</head>
<body>
<div data-role="page" data-cache="never">
<div data-role="content">
<h1>Page 1z</h1>
<a href="page2.html">Page 2</a>
<div id="test"></div><!-- this div should be removed upon going to the next page -->
<script>
function testContent() {
    // this function still exists on the next page, how can it be removed?
    alert("testContent");
}
function doPageShow() {
    alert("Page 1");
    alert($("#test").length); // shows 1 which is correct
    testContent(); // function is on this page, so it works
}
</script>
</div><!--content-->
</div><!--page-->
</body>
</html>

Page 2

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Page 1</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" />
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
<script>
$('div').live('pageshow',function(event, ui) {
    $(ui.prevPage).remove();
    doPageShow();
});
$('div').live('pagehide', function(event) {
    $(event.target).remove();
});
</script>
</head>
<body>
<div data-role="page" data-cache="never">
<div data-role="content">
<h1>Page 2</h1>
<a href="page1.html">Page 1</a>
<script>
function doPageShow() {
    alert("Page 2");
    alert($("#test").length); // shows 0 which is correct
    testContent(); // why does this still work???
}
</script>
</div><!--content-->
</div><!--page-->
</body>
</html>
3
  • 3
    Wrap you code in pageshow using .on and disable them on pagehide using .off. Note that .live is deprecated and replaced with .on. Commented Nov 26, 2013 at 21:20
  • I tried on, but it still persists. Commented Dec 5, 2013 at 16:23
  • try $(document).off("pageshow").on("pageshow", function () { //code }); this will remove previous bindings. another option, use $.mobile.changePage("page1.html", { reloadPage: true }); this will force reload page and removes previous objects and bindings. Commented Dec 5, 2013 at 16:42

2 Answers 2

1

Javascript objects live until the page refreshes. This is one of the advantages of jquery mobile, as parsing JS can take a long time on mobile devices, it is considered better to do it once.

If you really need to you could set the function to null.

Sign up to request clarification or add additional context in comments.

Comments

0

I think I figured this out. Basically in JavaScript a function is just another object like:

doPageShow = function(){...}

Everything set in javascript persists on subsequent ajax loaded pages, so if I set a variable in one page, it will still have that value in another ajax loaded page, including functions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.