0

page1.html

<!DOCTYPE html>
<html>
    <head>
        <script src="common_script.js"></script>
    </head>
    <body onload="init()">
        <h1>My First Page</h1>
        <p id="demo">This is a paragraph.</p>
    </body>
</html> 

page2.html

<!DOCTYPE html>
<html>
    <head>
        <script src="common_script.js"></script>
    </head>
    <body>
        <h1>My second Page</h1>
        <div id="nextpageId">I want to access my div</div>
    </body>
</html> 

common_script.js

function init() {
    alert($("#nextpageId").text());
}

In the following code I have two pages i.e. page1.html & page2.html and these contain the common JavaScript file.

My problem is to access page2.html div#id when page1.html is loading.

8
  • 1
    Though modern phones can do many things, this is impossible. There's no way to catch a page and an element in it from a server just with plain id.
    – Teemu
    Dec 31, 2012 at 10:58
  • 1
    why would you want to do that? http is stateless, if you want to retain state from one page to another, you need to use cookies and/or manage the state server side. Dec 31, 2012 at 11:07
  • Ok,thanks.how i solve my problem using jquery mobile.login page is page1.html and listview page is page2.html
    – user1903089
    Dec 31, 2012 at 11:09
  • Please explain your problem more clearly. Presumably Your login page will have a "login" button with a form containing user id and password that you validate server side. If login is valid, you send page 2 html back to the browser Dec 31, 2012 at 11:14
  • actually i have build the application using phonegap.I create the database with two tables i.e;login table and listvie table.When the app is first loading database is created.index.html page is first loading when the user click the submit button it goes to listview page.Problem is index.html is first loading i need get the id from the listview page.But id is not getting.Is any way to solve my problem...thanks.
    – user1903089
    Dec 31, 2012 at 11:26

2 Answers 2

0

use this in your javascript

$('body').attr('unique_id')

and give unique id for your page in body tag

<body id='unique_id'>

or you can also pass in url

or use this code

function init() {
    alert( $('div').attr('id') == "nextpageId" )
}
1
  • 1
    Thanks please give me one example.
    – user1903089
    Dec 31, 2012 at 12:21
0

You don't explicitly state that you are using jQuery Mobile but you have tagged your question as such and are using PhoneGap so I will assume so...

Your pages don't conform to a standard jQuery Mobile layout. They are missing the required data-roles. Specifically data-role="page" along with child data-role="content" declarations.

http://jquerymobile.com/demos/1.2.0/docs/pages/page-anatomy.html

Your second page will be loaded via Ajax and the <script> tag on the second page will be ignored. In fact, only the contents of the nested <div data-role="page"> section will be loaded.

To pull that markup, you can simply access the <div> given its ID and that ID should be unique. You can use the pageinit or pageshow events to control your timing on the Ajax load.

http://jquerymobile.com/demos/1.2.0/docs/api/events.html

Your Answer

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