• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

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

notfred

Lifer
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.
 
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>
 
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.
 
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.
 
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
 
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?
 
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.
 
Back
Top