views:

27

answers:

1

How can we use Jquery to read certain page content in a variable on a different page? If for example we want to read div.tag1 from a page into another page but in a variable( ie i dont want to load it into the page but manipulate it so store it in variable)

Eg: file1.html have(say)

<div class="tag1"> Hello world </div>
<div class="tag2"> Hello Boys </div>
<div class="tag3"> Hello Girls </div>

Now I want page2 to have Jquery script that will store the content of div.tag1 in a variable.

This is what I need in file2.html : // without loading it to any page ie without using load.

<script type="text/javascript">
var filename = "file1.html";
var myData = // Code to retrieve div.tag1 from file1.html( ie filename)
document.write(myData); // for the time being.. later it will go in Database.

Any help will be appreciated.

A: 

The simplest solution is to use load on a newly created element (a hidden element will also work):

var content = $("<div />").load("/page.html div.tag1",
   function(){ // callback
       alert(content.text());
   });
Kobi
Hello Kobi,The above code does not work. Can you tell me any other way to do this(without using .load) as the purpose is just to save it in some variable.(try to keep the filename also in some variable.)Thanks.
Hello. Can you please define "not work"? You need to use ajax anyway, and `load` is the most comfortable way of doing that.
Kobi
I am pretty new to Jquery to pardon me if I am wrong somewhere.The above code prints <localhost> in the alert box. Can you please explain little more so that i can move forward. The real purpose of this is that i want to read some particular tag from a page and store it in variable so that i can add it to database.
Can you post some more code? Edit your question, and add the urls of the pages, `div.tag1`, and your current jQuery code. A lot can go wrong here...
Kobi
The code that you mentioned was used as it is and it just pops up the alert box with message <localhost>.I am using Netbeans 6.8, Glassfish V3 server.This is what I need : <script type="text/javascript"> var filename = "file1.html" var myData = // Code to retrieve div.tag1 from file1.html( ie filename) document.write(myData); // for the time being.. later it will go in Database.</script>
I have edited the question also.