PDA

View Full Version : Showing HTML code of selection


Ben Chivers
07-02-2002, 02:08 PM
I am trying to do a javascript where the user selects text or an object on the same webpage and clicks a button to display the html code on this selection.

Is this possible?

Any help would be most appreciated.

Thanx in advanced

Many Regards,
Ben Chivers

Bosko
07-02-2002, 02:20 PM
Mozilla 1.1 supports this nativaly,just right click and select "View selection source".

joh6nn
07-02-2002, 02:34 PM
i'm pretty sure that's not possible using javascript. if it is, it's almost definitely not possible cross browser.

Bosko
07-02-2002, 02:42 PM
Maybe it is possible for IE and Moz using ranges,but it's useless since Moz already supports it.

joh6nn
07-02-2002, 02:51 PM
less than one percent of the internet world uses mozilla. on the other hand, better than 50% uses Explorer. Even if this built in functionality, were a part of Explorer, meaning that better 50% of the people, i still wouldn't just let it be, if i wanted it, because i want all of my users to have it. also, what if Ben wants to do something with it, that's not part of what's built in?

Vladdy
07-02-2002, 03:36 PM
Just curious,

what would be the purpose of such feature.... maybe there are other ways to do what you need.....

Ben Chivers
07-02-2002, 05:01 PM
I've used the following code but it just displays the selected text, not the actual html code. Heres the code:

<html>
<head>
<title>New Page 1</title>
<script>
function runscript(){
var sel = document.selection;
var rng = sel.createRange();
alert(rng.text);
}
</script>
</head>
<body>
<p><i><b>Some Sample Text</b></i></p>
<p><input type="button" value="Button" name="B3" onclick="runscript()"></p>
</body>
</html>

I was able to display the documents html code, but I didn't want to do that, I just wanted to display the html code of the selection. Here's the code I used for that:

<html>
<head>
<title>New Page 1</title>
<script>
function runscript(){
iHTML = document.body.innerHTML;
alert(iHTML);
var sel = document.selection;
var rng = sel.createRange();
alert(rng.text);
}
</script>
</head>
<body>
<p><i><b>Some Sample Text</b></i></p>
<p><input type="button" value="Button" name="B3" onclick="runscript()"></p>
</body>
</html>

Many Regards,
Ben Chivers