Using Addslashes and Stripslashes Functions
Author: Uzi Levitovitch
Type: Web Programming
JavaScript
Level: Beginner
Added: 25-02-2009
Rating:
Using Addslashes and Stripslashes Functions
If you are used to programming your server side code using PHP then you have probably used the addslashes() and stripslashes() functions to handle the processing of certain characters such as quotes within input fields. Javascript doesn't come with equivalent functions but you can easily add these functions into Javascript by adding the following code:
function addslashes(str) {
str=str.replace(/'/g,'\'');
str=str.replace(/"/g,'\"');
str=str.replace(/\/g,'\\');
str=str.replace(/ /g,'\0');
return str;
}
function stripslashes(str) {
str=str.replace(/\'/g,''');
str=str.replace(/\"/g,'"');
str=str.replace(/\\/g,'\');
str=str.replace(/\0/g,' ');
return str;
}
Adding to Favorites
Only Internet Explorer allows you to create a link that your visitors can use to automatically add your page to their favorites. With other browsers all you can do is to put a message on the page reminding them which key combination to press.
The first step in using this script is to select the code from the text box below (there is a highlight all button beneath it to make this easier) and copy it into a file called favlink.js.
var chr = 'CTRL-D';
var agt=navigator.userAgent.toLowerCase();
if(agt.substr(agt.indexOf('opera')+6,1) < 9) chr = 'CTRL-T';
if(window.external)document.write('<a href="javascript:window.external.AddFavorite(self.location,document.title)">Add to Favorites</a>');
else document.write('Press '+chr+' to bookmark this page.');
You then attach the script into your web page in the body section of your page where you want the link or message to appear using the following code:
<script type="text/javascript" src="favlink.js">
</script>
Enjoy the result!



