Jump to content

[SOLVED] safe text


jaymc

Recommended Posts

I am testing a live chat Im making and Ive hit a problem

 

Lets say someone wants to post some PHP code.. the javascript that handles the retreival of the text doesnt like it

 

For example if I try and send

 

<? echo "hi"; ?>

 

I dont get anything. It can deal with the quotes fine " ' so Im assuming its the tags < >

the javascript picks it up by geting the .value of the text field in a form and writes it to a div using innerHTML =

 

How do you fix this. php would use htmlspecialchars or something, whats javascripts approach

Link to comment
Share on other sites

Its not the slashes causing the problem though..

 

It appears to be this <

For JS alone, it's the quotes, NOT the slashes... the backslahes are for escaping.

 

The issue you're having is that innerHTML expects, well, HTML, so it seems < as an open tag.  I'm not sure why you're doing what you're doing, the php code won't ever get executed.  If you just want to display it, user innerText, not innerHTML.

Link to comment
Share on other sites

Ok, that works but the string contains both HTML which i need parsed and text string which cannot be parsed

 

Here, notice chat right at the very end, this will be the users text, this must not parse HTML

 

document.getElementById('messages').innerHTML += "<BR><a href=\"javascript:image(p.php?user=" + from + "')\">" + from + "</a> - " + chat

 

I tried this way around it but strangely its not working

 

 

document.getElementById('messages').innerHTML += "<BR><a href=\"javascript:image(p.php?user=" + from + "')\">" + from + "</a> - "

document.getElementById('messages').innerText += chat

 

When I do that, the innerHTML stuff just above the innerText stuff is not being parsed

 

Any ideas?

Link to comment
Share on other sites

That's because your final statement switches the entire thing back to text.  You either shouldn't be doing what you're doing -- i.e. just use html only, and write out another div on the inside with an id you can find, and then insert the user's text after it's been rendered -- or use DOM functions to make new element children.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.