window problem

V

vaib

hi there,
I have a problem coming up .
I want to write a javascript code such that I am able to refer to the
html source of some other page and extract something (say, some form
in its body) from it . As far as i've studied javascript I understand
that I'll have to open that page in some window and once I have its
window name then I can refer to anything in its source using getSource
function .
The problem is that I do not want to open that page in a new window .
Is there a way i can refer to the source of some page without opening
it in a window ?
Thanking in anticipation . Vaib .
 
E

Erwin Moller

vaib schreef:
hi there,
I have a problem coming up .
I want to write a javascript code such that I am able to refer to the
html source of some other page and extract something (say, some form
in its body) from it . As far as i've studied javascript I understand
that I'll have to open that page in some window and once I have its
window name then I can refer to anything in its source using getSource
function .
The problem is that I do not want to open that page in a new window .
Is there a way i can refer to the source of some page without opening
it in a window ?
Thanking in anticipation . Vaib .

Hi,

Is the page you want to leech/inpect in the same domain as the page that
holds your JavaScript?
You might have hitted the Same Origin Policy:
Read more here:
http://en.wikipedia.org/wiki/Same_origin_policy

I don't know what it is you are trying to build, but maybe using a
severside language makes more sense, eg: PHP, VB, Perl, Java, even
JavaScript can be used for that.

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
 
H

Hubert

hi there,
I have a problem coming up .
I want to write a javascript code such that I am able to refer to the
html source of some other page and extract something (say, some form
in its body)  from it . As far as i've studied javascript I understand
that I'll have to open that page in some window and once I have its
window name then I can refer to anything in its source using getSource
function .
The problem is that I do not want to open that page in a new window .
Is there a way i can refer to the source of some page without opening
it in a window ?
    Thanking in anticipation . Vaib .

hi,
let us assume, as Erwin pointed out, that the document running your
script and the document whose html you want are from the same domain.
Let us also assume that you do not want to use window.open, because
the user would be able to see that.
Then you could also create an invisible iframe in your current
document, load the required html document, wait some time for
completion, and then access innerHTML of the top html node.

Here is some sample code:

var iframe;

function loadPage( url ) {
// Create iframe node.
iframe = document.createElement( "iframe" );
// Let it be invisible.
iframe.style.display = "none";
// Append to current body.
document.body.appendChild( iframe );
// Load required url.
iframe.contentWindow.document.location = url;
}

function getHtml() {
var dom, html = "";
if( iframe ) {
// Get reference to our loaded document.
dom = iframe.contentWindow.document;
// Get requested html.
html = dom.getElementsByTagName( "html" )[0].innerHTML;
// Destroy our iframe again.
document.body.removeChild( iframe );
iframe = null;
}
return html;
}

Notes:

(1) the iframe is removed again, after the desired html has been
obtained.

(2) Since loading a document takes some time, you must not immediately
call getHtml after loadPage. Allow some time to pass or find out in a
different way when loading is complete.

Hubert
 
H

Hubert

hi there,
I have a problem coming up .
I want to write a javascript code such that I am able to refer to the
html source of some other page and extract something (say, some form
in its body)  from it . As far as i've studied javascript I understand
that I'll have to open that page in some window and once I have its
window name then I can refer to anything in its source using getSource
function .
The problem is that I do not want to open that page in a new window .
Is there a way i can refer to the source of some page without opening
it in a window ?
    Thanking in anticipation . Vaib .

hi,
let us assume, as Erwin pointed out, that the document running your
script and the document whose html you want are from the same domain.
Let us also assume that you do not want to use window.open, because
the user would be able to see that.
Then you could also create an invisible iframe in your current
document, load the required html document, wait some time for
completion, and then access innerHTML of the top html node.

Here is some sample code:

var iframe;

function loadPage( url ) {
// Create iframe node.
iframe = document.createElement( "iframe" );
// Let it be invisible.
iframe.style.display = "none";
// Append to current body.
document.body.appendChild( iframe );
// Load required url.
iframe.contentWindow.document.location = url;
}

function getHtml() {
var dom, html = "";
if( iframe ) {
// Get reference to our loaded document.
dom = iframe.contentWindow.document;
// Get requested html.
html = dom.getElementsByTagName( "html" )[0].innerHTML;
// Destroy our iframe again.
document.body.removeChild( iframe );
iframe = null;
}
return html;
}

Notes:

(1) the iframe is removed again, after the desired html has been
obtained.

(2) Since loading a document takes some time, you must not immediately
call getHtml after loadPage. Allow some time to pass or find out in a
different way when loading is complete.

Hubert
 
V

vaib

hi there,
I have a problem coming up .
I want to write a javascript code such that I am able to refer to the
html source of some other page and extract something (say, some form
in its body)  from it . As far as i've studied javascript I understand
that I'll have to open that page in some window and once I have its
window name then I can refer to anything in its source using getSource
function .
The problem is that I do not want to open that page in a new window .
Is there a way i can refer to the source of some page without opening
it in a window ?
    Thanking in anticipation . Vaib .

hi,
let us assume, as Erwin pointed out, that the document running your
script and the document whose html you want are from the same domain.
Let us also assume that you do not want to use window.open, because
the user would be able to see that.
Then you could also create an invisible iframe in your current
document, load the required html document, wait some time for
completion, and then access innerHTML of the top html node.

Here is some sample code:

var iframe;

function loadPage( url ) {
    // Create iframe node.
    iframe = document.createElement( "iframe" );
    // Let it be invisible.
    iframe.style.display = "none";
    // Append to current body.
    document.body.appendChild( iframe );
    // Load required url.
    iframe.contentWindow.document.location = url;

}

function getHtml() {
    var dom, html = "";
    if( iframe ) {
        // Get reference to our loaded document.
        dom = iframe.contentWindow.document;
        // Get requested html.
        html = dom.getElementsByTagName( "html" )[0].innerHTML;
        // Destroy our iframe again.
        document.body.removeChild( iframe );
        iframe = null;
    }
    return html;

}

Notes:

(1) the iframe is removed again, after the desired html has been
obtained.

(2) Since loading a document takes some time, you must not immediately
call getHtml after loadPage. Allow some time to pass or find out in a
different way when loading is complete.

Hubert

Well thank you all so much . I think i got my answers in the
discussion .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top