It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

Select and copy/past the source code

Select and copy/past the source code

Postby shuu on Fri Jan 04, 2008 11:10 am

Hi,

I would like to select all the source code of a occur page thanks to select_and_copy.js JavaScript and copy/past it into the <textarea> of the same page.
Example of page:
Code: Select all
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="select_and_copy.js"></script>
</head>
<body>
<h1>Ma page</h1>
<p><textarea name="message" rows="30" cols="80"></textarea></p>
</body>
</html>

I precise I don't want to do that by downloading again the source code. I would like to copy/past the current source code of a loaded page, not download the same page again.

Thanks for help and happy new year :)
shuu
 
Posts: 2
Joined: Fri Jan 04, 2008 11:01 am

Postby leonard on Mon Jan 07, 2008 1:28 pm

try this:
Code: Select all
<html>

<head>
<script type="text/javascript">
function pasteCode() {
  var pageContent = document.getElementsByTagName('body')[0].innerHTML;
  document.getElementById('pageConentID').innerHTML = pageContent;
}
</script>
</head>

<body onload="pasteCode()">
<h1>Ma page</h1>
<p><textarea id="pageConentID" rows="30" cols="80"></textarea></p>
</body>
</html>


You may view the page here:
http://devppl.chew.ch/pasteContent.html

cheers!
- leonard
:%s/^M//
There are 10 kinds of people:
Those who understand binary and those who don't.
User avatar
leonard
100+ Club
 
Posts: 147
Joined: Tue Dec 18, 2007 7:11 am
Location: Switzerland

Postby shuu on Mon Jan 07, 2008 4:40 pm

Thanks you, leonard :)
I have trying it on Safari but it's work better on Firefox. It's very useful, but I would like to get all the source page... I don't know if it's possible.
shuu
 
Posts: 2
Joined: Fri Jan 04, 2008 11:01 am

Postby leonard on Sat Jan 12, 2008 4:13 pm

OK, the magic word is, once again: AJAX.

Here is the example:
http://devppl.chew.ch/loadTemplateWithJ ... ource.html

cheers!
- leonard
:%s/^M//
There are 10 kinds of people:
Those who understand binary and those who don't.
User avatar
leonard
100+ Club
 
Posts: 147
Joined: Tue Dec 18, 2007 7:11 am
Location: Switzerland

Postby leonard on Sun Jan 13, 2008 8:04 am

A small addition:
IE works with innerText, but FireFox needs innerHTML, which interpretes the code.
So for firefox I needed to 'escape' the special characters:

Code: Select all
myDOMText = myDOMText.replace(/&/g,'&amp;');
myDOMText = myDOMText.replace(/</g,'&lt;');
myDOMText = myDOMText.replace(/>/g,'&gt;');

I didn't test the code on other browsers. Hope it works.

cheers!
- leonard
:%s/^M//
There are 10 kinds of people:
Those who understand binary and those who don't.
User avatar
leonard
100+ Club
 
Posts: 147
Joined: Tue Dec 18, 2007 7:11 am
Location: Switzerland


Who is online

Users browsing this forum: No registered users and 4 guests