在ColdFusion中查找和替换

时间:2015-01-10 12:23:29

标签: coldfusion

这是困扰我的代码。我需要找到代码并替换为Fancybox代码,但它会抛出错误:win未定义

替换mywin值

<cfsavecontent variable="x">
mywin = window.open (url,"win",'toolbar=yes,location=yes,resizable=yes,copyhistory=yes,scrollbars=ye‌s,width=878,height=810'); 
</cfsavecontent>

到此:

mywin = $.fancybox('href' :url,'width': '500');

我正在尝试这样

<cfset a  = Replace(x,"mywin = window.open (url,"win",'toolbar=yes,location=yes,resizable=yes,copyhistory=yes,scrollbars=yes,width=878,height=810');","$.fancybox()","one")>

更新代码:

<cfsavecontent variable="foo">
      function setmycode() {
      url = "http://myurl.com?thestep=9&sortBy=1&sortOrder=1";
      mywin = window.open (url,"win",'toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,resizable=yes,copyhistory=yes,scrollbars=yes,width=878,height=810');
    mywin.focus();
    return false;
    }
  </cfsavecontent>  
  <cfset a  = Replace(foo,"mywin = window.open (url,""win"",'toolbar=yes,location=yes,resizable=yes,copyhistory=yes,scrollbars=yes,width=878,height=810');","$.fancybox()","one")>
  <cfdump var="#a#">

1 个答案:

答案 0 :(得分:6)

您需要注意您的代码。即使你的问题中突出显示的语法实际上也指出了错误!

如果错误说明了&#34; win&#34;和文本&#34; win&#34;通过语法高亮显示突出显示...它有点遗漏,这就是错误所在。这就是你应该仔细检查的内容。

你有一个用双引号分隔的字符串,但字符串本身有双引号:

mywin = window.open (url,"win",'toolbar=yes,location=yes,resizable=yes,copyhistory=yes,scrollbars=yes,width=878,height=810');

因此CF解析器将"win"周围的第一个双引号视为字符串的结尾。然后接下来只是垃圾(并且会产生语法错误,正如您所看到的那样)。

如果你的字符串中包含字符串分隔符,则需要将它们转义。在CFML中,可以通过加倍来实现这一点,例如:""(&#34; Special characters&#34;)

所以你的字符串变为:

mywin = window.open (url,""win"",'toolbar=yes,location=yes,resizable=yes,copyhistory=yes,scrollbars=yes,width=878,height=810');

当分隔时,它现在可由CF解析:

<cfset a  = Replace(x,"mywin = window.open (url,""win"",'toolbar=yes,location=yes,resizable=yes,copyhistory=yes,scrollbars=yes,width=878,height=810');","$.fancybox()","one")>