View Full Version : Open and Copy
Ben Chivers
10-01-2002, 12:10 AM
I am trying to open an html file on my web site using javascript using window.open, I then want to copy the contents of this HTML webpage to the clipboard, and paste it in the current document, but it's not working.
I have been trying this for the last 4 hours and have got nearly nowhere!
Any help would be most appreciated!
Many Regards,
Ben Chivers
beetle
10-01-2002, 01:28 AM
Do you actually want to show the HTML as text? or render the rich HTML to the page?
whammy
10-01-2002, 01:51 AM
You can't do that using client-side scripting such as javascript.
With VBScript and ASP (server-side scripting) t's fairly simple (assuming you're using something that is compliant with VBS or ASP).
I am fairly sure this isn't much help to you at the moment -but go to webmonkey.com and check out their database tutorials. I think that's what you might really want to learn.:D
glenngv
10-01-2002, 07:59 AM
IE only.
<html>
<head>
<script language="javascript">
function doOpen(url){
myWin = window.open(url,"")
var myStr=myWin.document.documentElement.outerHTML;
//if you want to put the code in a textarea
document.f.t.value=myStr;
//or if you want to put the code in a div tag
myStr=myStr.replace(/\</g,"<").replace(/\>/g,">").replace(/\r\n/g,"<br>");
document.getElementById("code").innerHTML = myStr;
alert("HTML source of "+url+" pasted.")
}
</script>
</head>
<body>
<form name=f>
<textarea name=t rows=20 cols=100></textarea>
<div id="code"></div>
<input type=button value="Open" onclick="doOpen('anypage.htm')">
</form>
</body>
</html>
Alekz
10-01-2002, 08:47 AM
Hi,
whammy, You're wrong. It can be easily done through client side JavaScript, with some restrictions of course! I mean a function which takes two parameters: URL - the URL of the remote HTML You need and callback - the name of a function which will be called when the HTML is loaded. callback will recieve the remote HTML as a string parameter.
Here are the restrictions:
1. Does not work with IE4 at all
2. Does not work with NS6.0 at all (No problem with NS6.1+)
3. For All browsers - the URL loaded must be from the same site as the loading page.
4. For NS4 the URL passed have to be an absolute one (can also be a file URI)
5. Have not tryed this with Opera, I'll be very surprised if it could be done :)
6. In most cases it should not work through a HTTP proxy (have not tested)
7. Most probably it will not send cookies to the server, so You will not have session state for the remote loader (Not tested as well).
I'll appreciate any feedback on this function, especially for the cases I have not tested, so that I could improve it (post amail to a_minkovsky@hotmail.com). Credits go also to Tim Scarfe (http://developer-x.com), who sent me lots of usefull advices and code snippets.
Now, here's a sample:
<html>
<head>
<script language="JavaScript">
var browseCap = new BrowserCaps();
function BrowserCaps(){
this.Layers = (document.layers)?true:false;
this.All = (document.all)?true:false;
this.DOM = (document.getElementById)?true:false;
this.IE4 = (this.All && !this.DOM);
this.NS6 = (this.DOM && !this.All);
this.NS60 = false;
if(this.NS6){
var minVer = String(navigator.appVersion);
dotPos = minVer.indexOf('.');
minVer = minVer.substr(dotPos+1,minVer.length-dotPos);
if(parseInt(minVer) == 0) this.NS60 = true; //Wrong, they are all with minVer == 0 :(
}
this.Opera = (window.opera)?true:false;
}
function loadURL(){
document.frm.targ.value = 'Loading...';
loadHTML(document.frm.url.value,urlLoaded);
}
function urlLoaded(sHTML){
document.frm.targ.value = sHTML;
}
function loadHTML(sURL,callback){
if(!browseCap) browseCap = new BrowserCaps();
if(browseCap.Layers){ //NS4 -> sURL have to be an absolute URI
var res = '';
buffer = new java.io.BufferedReader(new java.io.InputStreamReader(new java.net.URL(sURL).openStream()));
while ((line = buffer.readLine())!=null) res += line + '\n';
if (buffer != null) buffer.close();
callback(res);
}
else if(browseCap.All){ //IE
if(browseCap.IE4){ //IE4
//No luck with this one :(
}
else{//IE5+
var buffer = new Option();
buffer.addBehavior("#default#download");
buffer.startDownload(sURL,callback);
buffer = null;
}
}
else{ //NS6.1+, Moz1+ ->>> NS6.0 does not recognize the XMLHttpRequest :( Nor was I abble to use java.io - access denied...
var buffer = new XMLHttpRequest();
buffer.open("GET",sURL,false);
buffer.send(null);
var res = buffer.responseText;
callback(res);
}
}
</script>
</head>
<body>
<form name="frm">
URL: <input type="text" name="url">
<input type="button" name="btn1" value="Load URL" onClick="javascript: loadURL()"><hr>
<textarea name="targ" cols="80" rows="30"></textarea>
</form>
</body>
</html>
Alex
whammy
10-02-2002, 12:51 AM
I still say it can't be done - because of all of the restrictions!
Ok... so it can if the client's browser meets a specific criteria, and they aren't using version x.x - but why waste time on stuff that doesn't work for most people?
I'd rather use something I know works (like a server-side scripting language)...
I could probably accomplish what he's asking (albeit it may be using a different method) with ASP and VBScript, or PHP without too much trouble. :)
Ben Chivers
10-02-2002, 07:29 AM
Thanks for all of your replies, I found glen's the easiest to use.
Many Regards,
Ben Chivers
Alekz
10-02-2002, 09:57 AM
Hi,
The restriction is IE 4.0 and NS 6.0 - I would not call their users "most people"... 2 browsers from 11...
The reason to waste time for this is the same for all client side dynamic pages - avoid reloading from the server that is already supplied to the client...
There was another thread about populating a combo box depending on the selection in another combo-box. The usual way is just to submit the page, thus reloading the contents of both comboboxes (or using frames, which is not always suitable). The right (for me) way is to make an RPC call to the server and get back only the data nedded to populate the second combo box, which is done with about 20 lines of code. Better? Or not?
Alex
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.