468,773 Members | 2,396 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,773 developers. It's quick & easy.

document.write-ing some innerHTML to a new window

I've got a way to get all of the source HTML for a given page by line
and detect where certain attributes exist, and I would like to write
the line of HTML code I've been working on into a new window. Right
now, the code looks something like this
---
function validate() {

// gets the HTML source of the document and splits it by line.
var allLines = document.documentElement.innerHTML.split('/n');

for (n=0;n<allLines.length;n++) {

// looks for opening tags, looks for STYLE attribute.
var openingTag = allLines[n].match(/</gi);
var styleAttribute = allLines[n].match(/style/gi);

if (openingTag && styleAttribute) {

// gives the line number, (n+1) and the HTML source ofline that has an
opening tag and //STYLE attribute. This works (even if this code I
wrote in 2 minutes has errors)
alert((n+1) + allLines[n]);

//Instead of an alert box, I'd like to write it to a document in a new
window. Unfortunately this //writes the output of the HTML (as if it
were a visual fragment of the page), not the HTML //source as I would
like. The line number shows up fine, but no source.
var newwindow = window.open('',errorwindow,iforget);
newwindow.document.write((n+1) + allLines[n]);
document.close;
}}}
---
So, does anyone know why the HTML source would show in an alert box,
but not in a document.write? Thanks.

Aug 17 '06 #1
3 1938
The actual code is the first section of code below. It will properly
bring up an alert box with any line of source that has a tag with a
STYLE attribute, and the line number of that particular element. What
I need, though, is for that information to be written to a document in
a new window. I've included a version of what I need, in case anyone
can suggest how that can be done.
---
Brings up an alert box with the source and line number:

function linenumber() {

var allLineNumbers = document.documentElement.innerHTML.split("\n");

for (var n=0;n<allLineNumbers.length; n++) {

allTags1 = allLineNumbers[n].match(/<[a-z].*/gi);
allTags2 = allLineNumbers[n].match(/style.*/gi);

if(allTags1&&allTags2){
alert((n+1)+allLineNumbers[n]);
}}}
---
Brings up new window and writes one of the line numbers, but not the
source:

function linenumber() {

var allLineNumbers = document.documentElement.innerHTML.split("\n");

for (var n=0;n<allLineNumbers.length; n++) {

allTags1 = allLineNumbers[n].match(/<[a-z].*/gi);
allTags2 = allLineNumbers[n].match(/style.*/gi);

if(allTags1&&allTags2){

errwin =
window.open('','errorwindow','width=525,height=375 ,scrollbars=yes');
errwin.document.write(n +allLineNumbers[n]);
errwin.document.close();
}}}

---
Thanks.

Aug 17 '06 #2

function linenumber() {

var docHTML = document.documentElement.innerHTML;

// match any tag (group \1) with a style sttribute (group \2)
// we know it's a tag when it has an ending tag
// this regulare expression doesn't match tags like <br style="" />
// because it has no ending tag (</brwhich is not valid HTML)
var strMatches = docHTML.match( /<(.*)\s*(style=).*>.*<\/\1>/gi );

// we we have matches
// could also look for strMatches.length 0
if ( strMatches ) {
// loop through them all...
for ( var i=0; i<strMatches.length; i++ ) {
// get their index within the original string
var index = str.indexOf( strMatches[i] );

// here it is important to replace special tag characters by their
html equivalent
document.writeln( "Match at " + index + " = " +
strMatches[i].replace( /</g, '&lt;' ).replace( />/g, '&gt;' ) );
}

} else {

document.write( 'No match found !' );
}

</script>

</pre>

</body>
</html>
Hope this helps.

Aug 17 '06 #3
hmm... I must've woke up that wrong side of the bed this morning...
can't copy/paste right... Well, the general idea is there...

Aug 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Ike | last post: by
13 posts views Thread by Stumped and Confused | last post: by
11 posts views Thread by Tony | last post: by
15 posts views Thread by Cerebral Believer | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.