replace spaces for %20 in javascript

Post Reply
robertvorselman
Newbie
Posts: 2
Joined: Thu Mar 31, 2011 9:39 am

replace spaces for %20 in javascript

Post 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












bens
Advanced member
Posts: 253
Joined: Thu Mar 03, 2011 10:13 am

replace spaces for %20 in javascript

Post 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
robertvorselman
Newbie
Posts: 2
Joined: Thu Mar 31, 2011 9:39 am

replace spaces for %20 in javascript

Post by robertvorselman »

Hi bens



thanks alot, it works :)
Post Reply