In the following code:
JavaScript
x
8
1
<script type="text/javascript">
2
function updateView(set) {
3
$.post("<?php echo base_url("/show_cards/load_page")."/"; ?>"+set, function( data ) {
4
$( "#content" ).html( data );
5
});
6
}
7
</script>
8
‘set’ is a string variable which can have spaces in it. I’m noticing when it has spaces it’s not working correctly. How can I fix this?
EDIT: For clarity, I’d like to keep the spaces intact.
Advertisement
Answer
You have to replace intermediate space (' '
) with '%20'
using replace()
, and eliminate boundary spaces (' '
) using trim()
:
JavaScript
1
9
1
<script type="text/javascript">
2
function updateView(set) {
3
set=set.trim().replace(/ /g, '%20');
4
$.post("<?php echo base_url("/show_cards/load_page")."/"; ?>"+set, function( data ) {
5
$( "#content" ).html( data );
6
});
7
}
8
</script>
9