Avatar of pepio
pepio
 asked on

Alternatives to innerHTML for use with a Quote link on a forum

I'm looking for a Alternatives to innerHTML what I can use on my forum system.

You can see in the code, what I'm using now.

Because I read a lot of negative response to innerHTML on the WWW, and it's not working when $quote3[post] contain a: " , ` '" " or other characters like that, I wanted to ask if you could help me with a better way of doing this.
<script type="text/javascript">
function insertText(val,e){
document.getElementById(e).innerHTML+=val;
}
</script>
 
<textarea id="post"></textarea><br />
 
<a href="javascript:insertText('$quote3[post] ','post');" onClick="void(0)">Quote</a>

Open in new window

PHPAJAXJavaScript

Avatar of undefined
Last Comment
pepio

8/22/2022 - Mon
Tomarse111

If its to go into that textarea, why not just use value?
<script type="text/javascript">
function insertText(val,e){
document.getElementById(e).value+=val;
}
</script>
 
<textarea id="post"></textarea><br />
 
<a href="javascript:insertText('$quote3[post] ','post');" onClick="void(0)">Quote</a>

Open in new window

pepio

ASKER
Better, but still don't work with a $quote3[post] like: "Hello, I'm ..." Because of the , and ' .
Tomarse111

Try escaping your PHP:


<a href="javascript:insertText(\'' . $quote3[post] . '\','post');" onClick="void(0)">Quote</a>

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
pepio

ASKER
No sorry, that didn't made the difference.

Removing the , and ' is not a option, because the weird post you would get.
Tomarse111

The above may not work, as I'm not a PHP programmer, as was pure guess work. Also, I'm not suggesting you remove the characters, I'm saying they need escaping, which would allow you to use value or innerHTML to your hearts content (There is nothing wrong with innerHTML as is faster than other w3c DOM methods).

Some handy links for you would be:
http://www.the-art-of-web.com/javascript/escape/
http://www.the-art-of-web.com/php/javascript-escape/

pepio

ASKER
That would indeed fix the problem with the ' and the ", but it doesn't fix the problem with the ,.

I would like to thank you for all your help, and I hope that someone who know how to fix it, will see my question.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
FatzStallone

I had problem using it once and resorted to loading page into iframe.

if(check.name.value==""){
self.frames['required'].location.href = 'gb_nr.htm'
return(false);
}

'required' is the name of the iframe.
pepio

ASKER
But I don't use frames...
FatzStallone

Lol, why not do you have an allergy? There's a stereotype against frames because of search engine issue but they can be navigated and in the end they don't have any affect at all on search engines.

This method works, the page I load into tiny iframe is 1kb and never an issue with browsers. I use these methods http://www.searchengineguide.com/dale-goetsch/optimizing-frames-for-search-engines.php and put noindex noarchive if I don't want them in search engines.
Your help has saved me hundreds of hours of internet surfing.
fblack61
pepio

ASKER
I'm not looking for a frame system. But for a way to get my quote link working....
Tomarse111

As i said above, you simply need to escape the unsafe JS characters in your PHP output, then use the script below. You certainly don't need to start using frames.
<script type="text/javascript">
function insertText(val,e){
document.getElementById(e).value+=val;
}
</script>
 
<textarea id="post"></textarea><br />
 
<a href="javascript:insertText('$quote3[post]','post');" onClick="void(0)">Quote</a>

Open in new window

Tomarse111

As i said above as well, my background is not from PHP, it's coldfusion. In CF we have a function called JSStringFormat which makes a string safe i.e. it basically replaces all unsafe chars with \ in front of them to escape them.

This is the sort of function you need in PHP. While looking around I have found:

http://snippets.dzone.com/posts/show/5342

This may help
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
pepio

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.