Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global String Replace function 1

Status
Not open for further replies.

Mighty

Programmer
Joined
Feb 22, 2001
Messages
1,682
Location
US
Hi Folks,

I am trying to do a global replace on the innerHTML of a DIV. The code below works fine when my regular expression is just /<TABLE/g but when I introduce the extra word it messes up the formatting. Can anyone tell me if there is a problem with it:

var re = /<TABLE BORDER/g;
var myCode = '<P STYLE="page-break-before: always;">My Heading</P><TABLE BORDER';
var HTML = Details.innerHTML.replace(re, myCode)

Mighty
 
Seems to work fine for me. What is getting messed up?

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
I have multiple tables on the page and it is ending up as one long table. Is there any problem with having spaces in regular expressions.

Mighty
 
Shouldn't be a problem. Do you have closing </TABLE> tags? Can you post the contents of the variable "HTML" or some of the code as it looks after your code executes? You can get it by putting the following code in your address bar and pressing enter:
Code:
javascript:void(window.open("javascript:document.open(\"text/plain\");document.write(opener.document.body.parentNode.outerHTML)"))

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
In this particular case I have four tables. Each start with:

The first table starts with:

<TABLE CELLPADDING=3 CELLSPACING=0 BORDER=0 STYLE="font: 8pt verdana">
.
.
.
</TABLE>

All subsequent tables start with:

<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0 STYLE="font: 8pt verdana;">
.
.
.
</TABLE>


When my regular expression is just /<TABLE/g it works fine and it adds the <P></P> tag before each table as expected. The problem is that I don't want it to display before the first table - that's why I am trying to use the <TABLE BORDER

However, when I change the regular expression to <TABLE BORDER the <P></P> tags don't get inserted into the code and ALL my tables are like:

<TABLE style="FONT: 8pt verdana" cellSpacing=0 cellPadding=3 border=0>
.
.
.
.
</TABLE>

It's changing around the order of the properties and not inserting the <P></P>

Any ideas????

Mighty
 
Basically, I have an asp program which displays a certain amount of data. It breaks the data into tables of a certain number of rows and each table appears on a separate page.

What I am trying to do is place a paragraph at the top of every page (page header) but only when the user prints the page. I know that I could do this when I generate the page and hide all the headers and then just make them visible for printing but I am trying to use javascript to add in the paragraphs on the fly as it gives a more accurate printing time which is what I need.

Mighty
 
What if you put a blank <SPAN id="header"></SPAN> tag before every table. Then use this function to add the headers:
Code:
function addHeaders(){
  var spans=document.getElementsByName("header");
  for(var i=0;i<spans.length;i++){
    spans.innerHTML='<P STYLE="page-break-before: always;">My Heading</P>'
  }
}

Instead of using JavaScript, a better option may be to have your ASP page generate the headers only when a parameter (for example, printWithHeaders) is passed to the page, then include this in your <HEAD> tag:
Code:
<link rel=alternate media=print href="yourPage.asp?printWithHeaders=true">

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Oops, spans.innerHTML should be spans.innerHTML

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
The spans worked a treat. Thanks Adam

Mighty
 
Might there be a problem putting p (a block element) inside a span (flow element)? It shouldn't validate, but divs shoud work.

Just a suggestion; if it works, don't fix it!

--Chessbot

"So it goes."
Kurt Vonnegut, Slaughterhouse Five
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top