Page 1 of 1

replace spaces for %20 in javascript

Posted: Thu Aug 11, 2011 12:23 pm
by robertvorselman
I am fairly new to Switch.



I am trying to retrieve an url from a xml file and then replace the spaces with %20.



i use this javascript :

var newURI = theURI.replace(/s/g,"%20");



Original Path in XML :

http://194.178.160.247/pp/XGram/PDF/Visit Cor Spierenburg3_989_989_HR.pdf



After :

http://194.178.160.247/pp/XGram/PDF/Vis ... 989_HR.pdf







instead of the percentage char it puts in a path.





I have no idea what to try next, hopefully someone has some tips for me :)





Thanks!



Robert













replace spaces for %20 in javascript

Posted: Thu Aug 11, 2011 12:32 pm
by bens
Hi Robert,



What you're seeing is because %2 has special meaning when logging to the Switch message pane. But this should not be a problem, because the path is only substituted in the log, not in the actual variable. You can prove this with this code:



var theURI = "hello world";

var newURI = theURI.replace(/s/g,"%20");

File.write( "C:/out.txt", newURI );

s.log( 1, newURI );



In the Switch log you will indeed see an incorrect string, but if you check the file at C:/out.txt, you will see that it contains the correct "hello%20world".



Ben

replace spaces for %20 in javascript

Posted: Fri Aug 12, 2011 9:16 am
by robertvorselman
Hi bens



thanks alot, it works :)