Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old August 17th, 2006, 12:35 PM
e271828
Guest
 
Posts: n/a
Default 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.

  #2  
Old August 17th, 2006, 01:35 PM
e271828
Guest
 
Posts: n/a
Default Re: document.write-ing some innerHTML to a new window

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.

  #3  
Old August 17th, 2006, 03:25 PM
Yanick
Guest
 
Posts: n/a
Default Re: document.write-ing some innerHTML to a new window


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.

  #4  
Old August 17th, 2006, 03:25 PM
Yanick
Guest
 
Posts: n/a
Default Re: document.write-ing some innerHTML to a new window

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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,248 network members.