Is it possible to load a webpage as a string using javascript?

notfred

Lifer
Feb 12, 2001
38,241
4
0
Is it possible to do something like this in javascript:

var aString = getURL("http://www.somesite.com/somepage.cgi");

and have "aString" end up containing the content of the URL?

*NOTE: getURL() does not exist (as far as I know), I just created it as an example of what I want to do. Don't tell me "just use the getURL function in your example", please.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: ArmchairAthlete
I could tell you how to do it in Python, but I'm not sure about Java.

Javascript. I want to do it from a client's browser.
 

Modeps

Lifer
Oct 24, 2000
17,254
44
91
erm... load an external website as a string through JS? I dont think it can be done.
 

DnetMHZ

Diamond Member
Apr 10, 2001
9,826
1
81
Let me clarify exactly what you want to happen.

You want the source of the page you're requesting to be placed in the striing variable?

example

<html>
blah
blah
...
..
</html>
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Well, a page can link to a javascript file, so if that script file really triggered script code on that server, it could dynamically generate a response that includes the string you want. That pushes the getURL off to whatever script code is generating the response.


that is,

<script language="javascript" type="text/javascript" src="/dynagen/dyanmic.cgi?stuff=nonsense&amp;puffin=penguin">

and on the server it builds a js file on the fly that includes the line

var aString = " **** result server fetched with script code *** " ;


I have no idea whether any browsers block src= lines like that in script includes for security reasons.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
I don't think something like getUrl that goes and retrieves a resource by itself could exist because I've never heard of any networking library specifications in javascript. But if I had to take a whack at it here's what I'd try:

1. pop up another window pointing to the url you want
2. use the window object to somehow retrieve the text of that window (don't know if it's possible but it's seems feasible)
3. close the window

On second thought, maybe you could do it with an iframe instead of a new window, that would lead to less user annoyance and less likelyhood of being interupted by a popup blocker.
 

Beau

Lifer
Jun 25, 2001
17,730
0
76
www.beauscott.com
Originally posted by: kamper
I don't think something like getUrl that goes and retrieves a resource by itself could exist because I've never heard of any networking library specifications in javascript. But if I had to take a whack at it here's what I'd try:

1. pop up another window pointing to the url you want
2. use the window object to somehow retrieve the text of that window (don't know if it's possible but it's seems feasible)
3. close the window

On second thought, maybe you could do it with an iframe instead of a new window, that would lead to less user annoyance and less likelyhood of being interupted by a popup blocker.

Can't do it unless the file resides on the same host as the calling document. It's a security measure.

Example of how it could be done
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Huh. Nice example, I notice it grabs only the contents of the <body> tag and inserts applied style information. Did you just whip that up?
 

Beau

Lifer
Jun 25, 2001
17,730
0
76
www.beauscott.com
Originally posted by: kamper
Huh. Nice example, I notice it grabs only the contents of the <body> tag and inserts applied style information. Did you just whip that up?

Yeah, it only get's the HTML of the body. I'm not sure if I can get the <head> of the document.

I've used that method before when needing to pull form information to a dynamic form and the client didn't want to have the screen refresh.