document.write-ing some innerHTML to a new window

E

e271828

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;
}}}
 
E

e271828

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();
}}}
 
Y

Yanick

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 (</br> which 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 );

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

} else {

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

</script>

</pre>

</body>
</html>


Hope this helps.
 
Y

Yanick

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top