This is a discussion on replace \ within the JavaScript Programming forums, part of the Programming & Scripting category; I have a JS function that seems to be working fine until a value is added to a hidden text ...
| |||||||
|
#1
| ||||
| ||||
| I have a JS function that seems to be working fine until a value is added to a hidden text box. The function causes no problems until a value is added to the txt_filepath textbox, which has visibility:hidden style added to it. I think the problem is linked to the fact that the value in the textbox is in the form of \1\randomletters. I think it's the \'s that are causing the problem. How would I do a replace on them in JS? The error I get in Firefox is AddTask is not defined. The JS function is as follows:- Code: function AddTask(){
var tr_box = document.getElementById('tr_box_start1');
//alert(tr_box);
if(tr_box!=null){
document.getElementById('tr_box_start1').style.display='none';
document.getElementById('tr_box_start2').style.display='none';
}
document.getElementById('ctl00_main_content_update_progress').style.display='';
document.getElementById('update_text').innerHTML='Adding Task....';
var pcuser = document.getElementById('ctl00_hdn_pcuser').value;
var defaulttask = document.getElementById('ctl00_main_content_ddl_defaulttask').value;
if(defaulttask=='') defaulttask=0;
var visitorref = document.getElementById('ctl00_main_content_ddl_visitorref').value;
if(visitorref=='') visitorref=0;
var introref = document.getElementById('ctl00_main_content_ddl_introref').value;
if(introref=='') introref=0;
var cat = document.getElementById('ctl00_main_content_ddl_category').value;
var sendto = document.getElementById('ctl00_main_content_ddl_sendto').value;
var dtDue = document.getElementById('ctl00_main_content_txt_datedue').value;
var tmSpent = document.getElementById('ctl00_main_content_txt_timespent').value;
var title = document.getElementById('ctl00_main_content_txt_subject').value;
//alert(title);
var filepath = document.getElementById('ctl00_main_content_txt_filepath').value;
filepath=escape(filepath);
alert('filepath: '+filepath);
var note=FTB_API['ctl00_main_content_ftb_mess'].GetHtml();
//alert(note);
//return false;
TaskAdd.AddTask(pcuser,defaulttask,visitorref,introref,cat,sendto,dtDue,tmSpent,title,filepath,note,OnAddTaskComplete,OnError,OnTimeout);
}
__________________ Join the folding team |
|
#2
| ||||
| ||||
| The backslash is an escape character in JS, so that could possible be causing the problem. Here's how to replace in JS: JavaScript replace() Method To keep the backslash in the string, simply replace it with two backslashes.
__________________ jmurrayhead If you agree with me... click the icon! If my post solved your problem, click the button in the lower right-hand corner of the post.If you like it here...throw us a few bones to help support us. Join our Folding team: DeveloperBarn Folding |
|
#3
| ||||
| ||||
| That's what I think J, but the problem is getting the syntax right on the replace function. This gives an error of unterminated regular expression literal Code: document.write(str.replace(/\/,"\\")); Code: var str="Visit \Microsoft!"; document.write(str.replace(/\\/,\\\\) |
|
#5
| ||||
| ||||
| Nope. It doesn't work if the original string just has a single \ in it. So, this:- Code: var str="Visit \\1\\Microsoft!"; document.write(str) If I do:- Code: var str="Visit \\1\\Microsoft!"; document.write(str.replace(/\\/g,"\\\\")); If it starts with:- Code: var str="Visit \1\Microsoft!" Any other ideas? |
|
#10
| ||||
| ||||
| Hey R, Maybe if you convert to forward slashes first: Code: function back2forward(dataStr) {
return dataStr.replace(/\\/g, "/");
}
Code: function forward2back(dataStr) {
return dataStr.replace(/\//g, "\\");
}
|
![]() |
|
| Bookmarks |
| Tags |
| javascript, replace |
| Thread Tools | |
| Display Modes | |
| |