Link to home
Start Free TrialLog in
Avatar of Coast Line
Coast LineFlag for Canada

asked on

Escapting all Characters coming from popup to parent

Hi, I have the following line!

var theString = focument.getelementbyid('textarea').value;
returnvalues = encodeURIComponent(str);

now the it is not escaping all the characters, it is just returning the characters as it it while i want it to get converted into a safe javascript so i can use in my form.

Please guide
Avatar of gdemaria
gdemaria
Flag of United States of America image

are you getting any javascript errors?

it is case sensitive, the first line should be...


 document.getElementById('textarea').value


Assuming the ID of your field is "textarea"


Also, your second line does not use the variable from your first line, it is different !


var theString = document.getElementById('textarea').value;
returnvalues = encodeURIComponent(theString );

or 

function convertMe(obj) {
  obj.value = encodeURIComponent(obj.value);
}

<textarea onChange="convertMe(this);">

Open in new window

Avatar of balatheexpert
balatheexpert

just check if you have to use "theString" in the secondline argument instead of "str".

just a guess!!!

thanks,
bala
Avatar of Coast Line

ASKER

well i know its case-sensitive!

The data is passed to the parent window in url only thing is it is not rendering the special characters which is breaking up my code!, not doing any inserts!

var theString = document.ggetElementById('textarea').value;
returnvalues = encodeURIComponent(theString);
try if you have something like converttostring before you assign it to the returnvalues..

again a guess!!
what is this a Javascript function :(
tat's what i thought, it should have something like this right?? ;-) not not helpful, sorry!!!
So exactly how do i do, encodeURIComponent() function is not working in IE 7, if i use escape it works, but this does not, because this function handles all special characters
better u seek attention of some other expert!! dint get any trick!! :-(
hi, can we make a custom js function using replace and then use it agaian to convert back from what we converted and use that rather than using inbuilt functions of javascript
yea, u can try that i believe!!!
any Experts can Guide me A GUru on Javascript

> well i know its case-sensitive!

But your code had mistakes, that's why it didn't work


>  The data is passed to the parent window in url only thing is it is not rendering the special characters which is breaking up my code!, not doing any inserts!

What does this mean?    What happened when you tried the code?


The following works, enter your text in the first box and see it change to the 2nd one and change back to the third

<br>
<textarea id="before" rows="8" cols="50" onchange="document.getElementById('after').value=encodeURIComponent(this.value);document.getElementById('back').value=decodeURIComponent(this.value);"></textarea>
<br>
<br>
<textarea id="after" rows="8" cols="50"></textarea>
<br>
<br>
<textarea id="back" rows="8" cols="50"></textarea>

Open in new window

so how i implement it in my scenario!

Click on a link, a modal window opens!, i enter all bloody special characters and then onclick on a button, i am calling to function to go back to parent page and close modal window.

So it as above i get the value of the textarea in the theString value and pasing it to the event, which pass it to the parentwin dow in URL,

There it finds special charactes and break in between.

The code I provided answers your question, it shows you how to remove special characters and then put them back.

Now, you are talking about a specific situation of passing date through a URL and model window to a parent or something else, but you won't show any code.   How can we help you?   Do you want us to guess what your code looks like?    The code makes URL safe values that you should be able to pass.   If you need help, post your code.

An alternative..
If you are passing to a CFIDV or CFWINDOW then just use ColdFusion.Navigate and Post the form.  That way you don't need to pass things on the URL.


Here is the same Code what i am trying!

<script>
function popup(name,id,comments)
     Window.ShowModalDailog("pages.cfm?name="+name+"&id="+id+"&comments="+comments,window,"attributes of width & height");
    var temp = comments; [this value will come from the comments which will be rendered back from the popup page]
    window.open = "pathtothepage.cfm?temp=+temp";
</script>


<a href="javascript:popup('#name#','#id#','#comments#');>Click</a>

the temp will be used in the page below, so it will show you the value from the url in the temp variable expand with the value coming from popup
something like

<textarea id="before" rows="8" cols="50"></textarea>

on the focus lose of the textarea or click on save, it should on runtime using Coldfusion should convert the data in urlencodedformat and then i pass through the url and then i can decode it

is that possible
Any Updates
Any Update on this :-
Are you saying that you don't know how to put the function I have you into your code?

You could do this...

<script>
function popup(name,id,comments)
     Window.ShowModalDailog("pages.cfm?name="+name+"&id="+id+"&comments="+encodeURIComponent(comments),window,"attributes of width & height");
</script>


 I have no idea what these statements are trying to do, so I just removed them...

    var temp = comments; [this value will come from the comments which will be rendered back from the popup page]
    window.open = "pathtothepage.cfm?temp=+temp";
ok i have to explain it you again

Scenario 1:

I click on parent Page & modal Window Opens, Values are Passed in the modal window...

2. i enter value in textarea which is inside popup window and click save, on save the value is transferred from modal window to parent through url..

3. Important POint, I enter all special characters in the textarea, and it pass through the url.

4. i want to encode/decode and use it for safely through url so that no value is truncated!

5. javascript encodeuricomponent is not working on IE 7. i am trying to use coldfusion urlencodedformat and urldecode..


If the javascript call is not working in IE 7 and you want to use Coldfusion's command for this, just use it on the Coldfusion variable...

On this line, you reference the CF variable #comments#  - so just wrap it in the urlEncodedFormat() function...
<a href="javascript:popup('#name#','#id#','#comments#');>Click</a>

Like this..
<a href="javascript:popup('#name#','#id#','#URLencodedFormat(comments)#');>Click</a>
That i am already doing, what the problem, when i retrieve back the special characters from popup, it breaks!

Hmm! one minute can i use onpropertyChange Method and once it starts typing store value hidden field and use that instead of textarea..

what you say!

> Hmm! one minute can i use onpropertyChange Method and once it starts typing store value hidden field and use that instead of textarea..

You could do that, but don't you have some action the user must do to close the pop-up window?

Would the user click "Save" or "Close" or something?   You can do it on THAT event instead of onChange...

Of course showing your code always help...
yes i have the button, the button is save & close


Save and Close have a function which is called to close the popup and transfer the contents to the aprent window through the url

perfect, just wrap the function around the comment variable !
how?

Just like I showed you before...



   encodeURIComponent(comments)

Just replace "comments" with the actual variable you're using, or the reference to your text box

Put it in the code you said you have (but won't show) where the user clicks the button to return the content of the text field back


 I am happy to modify your code to show you..
ok i am doing a pastbin exp[ire in 1 hour, please check

http://pastebin.com/w9n7MAHh
ASKER CERTIFIED SOLUTION
Avatar of gdemaria
gdemaria
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
this was i doing but it is not encoding the values, that why i posted the question

Is IS encoding the values, all you need to do to test it is add an alert() to show you on the screen.

Look at this..

If you click test it, you will see the encoded value.  It works fine.


<script>
function testIt () {
  var retVal = new Object();
  var str = document.frm.instructions.value;
  retVal.comments = encodeURIComponent(str);
  alert(retVal.comments);
  window.returnValue=retVal;
  window.close(); 
}
</script>

<form name="frm">
  <textarea id="instructions" name="instructions" rows="8" cols="50">It's is a <strong>test</strong> of $Values!</textarea>
</form>

<br>
<a href="#" onclick="testIt();return false;">Test It</a>
<br>

Open in new window

i have already followed this method and alerted being applied, but it does not render the special charcaters and pass the characters as it is, asi type in textarea

which character is not being rendered correctly, I will look for that character
All Special Characters
You are impossible to help, I don't know why I am trying


When I put in this....

<>!@#$%^&+


I get back this....

%3C%3E!%40%23%24%25%5E%26%2B


So obviously it is translating special characters.

you get this in alert right

ok, now the windw is closed and i get the value in the URL!

and then how i decode it using decodeURiComponent of javascript!

:(
Your responses are veryconfusing ...

The original question was how to encode special characters. I tested the code gdemaria provided it does properly encode special characters.  So before moving on to another question, does it work for you?

I think i am confusing you all guys!

Let's Make it simple

1. i open a popup using modal dialog through the i enter value in textarea in the modal window
2. i enter all speacial charactes
3. i close it and get an alert of special characters encoded,
4. now the page is reloaded with special characters in URL
5. i decode it using Javascript or Coldfusion, - i need to insert the values in DB.
5.1   - while the modal window is closed, i get special charactres, the coldfusion code is at top which inserts the url immediately to DB.

so how to convert the code in to same format back to insert it
> The original question was how to encode special characters.
> So before moving on to another question, does it work for you?

You still didn't answer the question.  Does the encoding work for you?
No it does not work for me

i tried all ways but nonetheless it not working
Ok. Give us a specific example of a string that does NOT work for you.  

sample string:           (your string)
actual results:           ?
expected results:      ?
my string!

~!@#$%^&*()_++__)_(*&^%$###!~!@#$;'[]/.,<>?:"{}

it does not work for me as i enter in textarea, it comes as it is in alert box
expected result should be the urlencodedformat where i can decode it later simple

as you can see from my code, the function to encode does work.   So there must be something wrong with your code.

You need to show the code block that uses this


agx, thanks for joining
will do in morning, too late here



   After all that, you give a "B" Grade...


   Something to remember for future threads...

i did not gave "B"

that's what it says...
oops "My Mistake", i was choosing "partially" as complete answer so wrongly i checked the checkbox for "B". My mistake \

Really This question Deserves "A" Grade

Call to Mods, please change the rating of this question
cool, thanks.  You can just click "Request Attention"