/    Sign up×
Community /Pin to ProfileBookmark

Storing remote HTML source code in local JavaScript variable.

I am trying to find a way in client side JavaScript (my ISP does not support SSI’s) to store the HTML source code of a given web page in a JavaScript variable.

I want to do this to filter out a pop up ad from a served piece of HTML before displaying it in an iframe.

The closest I got was :

<head>
function ViewSource() {
window.location = “view-source:” + “http://www.whatever.com“;
}
</head>

<body>
<a href=”javascript:ViewSource()”>View Source</a>
</body>

This simply duplicates the view source menu item. I was hoping there may be a replacement for the “view-source:” part of it, something along the lines of “extract-source:” and have a variable name instead of window.location

I know the address of the HTML code I want to manipulate and it is currently loaded directly into the iframe from that address. Problem is, the HTML code that is loaded into the iframe from that address contains a pop-up ad.

I want to store the contents of the HTML source into a JavaScript variable, I want to then remove the pop-up ad code from that variable, and then load the updated contents of that variable (original HTML code minus the pop-up ad code) into the iframe.

I do not want to display the HTML source in the iframe, I want to manipulate the HTML source and load it into the iframe as HTML code instead.

Any ideas?

Thanks,
Nick S.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@TheBearMayMay 03.2004 — You may want to look at this thread and see if it gives you any ideas:

http://forums.webdeveloper.com/showthread.php?s=&threadid=33866
Copy linkTweet thisAlerts:
@nicksauthorMay 05.2004 — Thanks very much TheBearMay. Vladdys slick getFile routine is exactly what I was after.

I changed the mime type to text/html and call getFile with a http address.

i.e var htmlsource=getFile("http://www.site.com/file.html");

I now just have to brush up on my Java string manipulation skills to filter out the bit of code I dont want!

I assume I can load the iframe with the variable by setting src="document.write(htmlsource)"?

There is a security issue though. The security setting "Access data sources across domains" needs to be enabled to stop a warning dialog from appearing.

"This page is accessing information that is not under its control. This poses a security risk. Do you want to continue?"


What kind of security risks does it pose?

As users of my website may have this setting disabled, does anyone know if there is another method of extracting HTML source from a remote address without this security issue?
Copy linkTweet thisAlerts:
@ShrineDesignsMay 05.2004 — use server side scripting (cgi, asp, php, etc.)
Copy linkTweet thisAlerts:
@nicksauthorMay 07.2004 — @shrinedesign, I do not have access to SSI's, my ISP does not support them.

Anyway, I had it all working when running the web site from my local drive, but as soon as I loaded it onto my web server, it no longer works, the following routine returns null from the web server, and works fine from a local machine.

[CODE]

<script language="JavaScript" type="">
<!--
function getFile(filename) {
oxmlhttp = null;

try {
oxmlhttp = new XMLHttpRequest();
oxmlhttp.overrideMimeType("text/html");
}
catch(e) {
try {
oxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e) {
return null;
}
}

if(!oxmlhttp) return null;

try {
oxmlhttp.open("GET",filename,false);
oxmlhttp.send(null);
}
catch(e) {
return null;
}

return oxmlhttp.responseText;
}
//-->
</script>

[/CODE]


Does anybody know how to achieve the result of the above routine without it returing null when running on a web server?

Thanks,

Nick S.
×

Success!

Help @nicks spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...