Click to See Complete Forum and Search --> : source code
ai3rules
05-01-2003, 10:18 PM
I was wondering if there was a way either with HTML or Javascript that would let me get the source code of the page, and display it - either on the page somewhere, an iframe, or in notepad........someone please let me know, thank you.
This is the best that I know you can do. It will only get what's between the <HTML> tags, however...
alert(document.getElementsByTagName("html")[0].innerHTML);
That method is, however, IE only, whereas the one that I posted also works in NN.... Guess it depends on if you need it to work cross-browser...
ai3rules
05-02-2003, 09:52 AM
that worked fine. is there a way however to get a specific chunk of the source?
AdamGundry
05-02-2003, 09:58 AM
The easiest way is probably to assign an ID to the containing element of the code you want, then use document.getElementById("id").innerHTML.
Adam
ai3rules
05-02-2003, 10:06 AM
can you elaborate on that a bit - how do i assign an id to a part of the code?
AdamGundry
05-02-2003, 10:15 AM
Put the following in the HTML tag around the code you need to access:
id="example"
If you have no tag surrounding the code, put a <div> or <span> around it like this:
<div id="example">
Code you want to read.
</div>
Adam
ai3rules
05-02-2003, 10:22 AM
ok, that worked. Thanks alot!!
ai3rules
05-02-2003, 10:41 AM
ok, i know this looks crazy and worthless and thats because it is. im just practicing the for loop, and trying to do something which im not sure is possible.
function TextGenerator()
{
var b = prompt("Enter Amount Of Boxes")
var c = prompt("Enter Size Of Boxes")
var d = prompt("Enter Background Color Of TextBox")
var e = prompt("Enter Font Color")
for (a = 1; a <=b; a++)
{
document.write("<input type='text' size='"+c+"' style='background: "+d+"' style='color: "+e+"'>" +"<br>");
document.write("<input type='button' value='Get Source Code' style='border: none'
}
}
now everything works fine up to the part where i wanna get the source code on the page that the text boxes are written to.
is it possible to do that? possibly have an id assigned to code thats created on the new page? let me know, thanks.
AdamGundry
05-02-2003, 12:10 PM
Yes, just add the <div> tag before the document.write() calls, and </div> afterwards:
document.write('<div id="example">');
then
document.write('</div>');
The ID should then be available in the new page.
Adam
ai3rules
05-02-2003, 12:24 PM
document.write('<div id="example">' +"<input type='text' size='"+c+"' style='background: "+d+"' style='color: "+e+"'>" +"<br>");
document.write('</div>' +"<input type='button' value='Get Source Code' onClick='alert(document.getElementById('example').innerHTML)'>");
like that? i did that and when i ran the script first of all there was a syntax error, and it also put a source code button underneath each text box.
AdamGundry
05-02-2003, 02:54 PM
You need to write the <div> tags and the button outside the for() loop.
Adam
ai3rules
05-02-2003, 04:31 PM
how exactly do i do that? i cant seem to figure out how to do it.
AdamGundry
05-03-2003, 03:15 AM
Something like this (untested):
function TextGenerator()
{
var b = prompt("Enter Amount Of Boxes")
var c = prompt("Enter Size Of Boxes")
var d = prompt("Enter Background Color Of TextBox")
var e = prompt("Enter Font Color")
document.write('<div id="getcode">');
for (a = 1; a <=b; a++)
{
document.write("<input type='text' size='"+c+"' style='background: "+d+"' style='color: "+e+"'>" +"<br>");
}
document.write("</div><input type='button' value='Get Source Code' style='border: none'>");
}
Adam
ai3rules
05-03-2003, 01:51 PM
function TextGenerator()
{
var b = prompt("Enter Amount Of Boxes")
var c = prompt("Enter Size Of Boxes")
var d = prompt("Enter Background Color Of TextBox")
var e = prompt("Enter Font Color")
document.write('<div id="getcode">');
for (a = 1; a <=b; a++)
{
document.write("<input type='text' size='"+c+"' style='background: "+d+"' style='color: "+e+"'>" +"<br>");
}
document.write("</div>" +"<input type='button' value='Get Source Code' style='border: none' onClick='alert(document.getElementById('getcode').innerHTML)'>");
}
the source code button showed up alright, but am i putting that onClick string in the wront place? thats not working. is it b/c when it makes the new page there are no HTML tags?
AdamGundry
05-03-2003, 02:12 PM
It's a quotes problem I think - try the line below:
document.write('</div><input type="button" value="Get Source Code" style="border: none" onClick="alert(document.getElementById(\'getcode\').innerHTML)">');
That should work - if not, put the alert() code in a seperate function.
Adam
ai3rules
05-03-2003, 03:07 PM
hey, thanks again. it worked.
only problem was when i retrieved the source with that button, it altered the source a bit.
<input style="background: #2e3036; color: white" size=10 type?text?><br>
thats what it gave for the source, when i retrieved it using the button after running the script.
__________________________________________________________
<input type'text' size='10' style='background: 2e3036' style='color: white'><br>
thats obviously how it is written if i look at the source the good old way.
is there a reason for that?
(hope im not a pain in your ass at this point lol, i really appreciate your help so far)
AdamGundry
05-04-2003, 02:01 PM
Sorry about the delay in replying. I don't know why the code is changing - I'm guessing it's to do with the browser rendering it. If the code has to be exactly as generated, you could store it in a variable, otherwise there's no real problem.
Adam
ai3rules
05-04-2003, 02:03 PM
no problem, ok ill try that.