15

I want the source code of an HTML page (1.html) to be used in another page (2.html). Furthermore, I want to perform operations on it in 2.html.

Is there a way to do this?

EDIT: 1.html is a separate public webpage and I do not have access to make changes to its source code. I have to do whatever I need only by using 2.html.

3
  • What kind of operations? Commented Feb 29, 2012 at 10:39
  • Do you want to get the whole content of 1.html page from 2.html using jQuery? You could make an AJAX request and get it if I understood right what you want to do. Commented Feb 29, 2012 at 10:40
  • @Pekka: Simple operations like extracting some innerHTML from 1.html and displaying it into 2.html Aldo 'xeon': its a cross domain request, so AJAX does not work. Commented Feb 29, 2012 at 10:42

6 Answers 6

23

To get the DOM converted to a string:

document.getElementsByTagName('html')[0].innerHTML

Question: what do you mean by "use it"? Do you need to include 1.html inside 2.html? Or do you just need to process it?

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

7 Comments

Poster wants a different pages HTML.
i just need to process it.. I have access to change the source code of only 2.html..
.innerHTML doesn't actually return the source code, it generates html to correspond to the current DOM tree. Subtle difference, but still...
a source code is the unprocessed html that the website developer wrote. innerHTML would give the DOM converted into a string. A DOMed html would have been fixed for html errors (with varying degree) and javascript changes would be refledted in it.
reflected. Also, check the OP's comment to his own post: he needs elements from that page to display them again somewhere else. The correct DOM here suits him best. Asking for the "source code" of a page isn't really feasible unless you have direct access to the original, unprocessed, unfiltered by the server, file. I hope he's fine now, 2 years after.
|
3

Its very simple

On 2.html use this jQuery snippet

$.get("1.html", function(response) { 
    alert(response) 
    //do you operations
});

Comments

3

jQuery:

$.get('ajax/test.html', function(data) {
  $('.result').html(data);
  alert('Load was performed.');
});

Comments

2

I don't understand whatyou mean that you must make modifications, but you could simply load the second page through AJAX

var url ="1.html";
$.ajax({
  url: url,
  dataType: 'html'
  success: function(data){
             //do something with data, which is the page 1.html
           }

});

Comments

2

Use can use .html method alternatively to get the entire html data of the page.

$(function(){
    var a = ($('html').html())
})​

Comments

0

A realy simple and modern way is the follow:

fetch('1.html').then(function(response) {
    /* when the page is loaded convert it to plain text */
    return response.text()
}).then(function(html) {
    /* initialize the DOM parser */
    var initParser = new DOMParser();
    /* parse the text */
    var parse = initParser.parseFromString(html, 'text/html');
    /* you can now even select part of that html as you would in the regular DOM */ 
    /* example */
    var docOutput = parse.querySelector('html').outerHTML;
    console.log(docOutput);
}).catch(function(error) {  
    console.log('Error fetch page: ', error);  
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.