Abbadon
Abbadon

Reputation: 2713

Trim not working in Coldfusion 8.0

I am trying to trim a string returned by one of my Coldfusion component but whatever I do Coldfusion add line feed at the start of the String without any reason resulting in an error in my Javascript. Do you have any idea of what's wrong with this code?

function back(){            
        window.location = <cfoutput>"#Trim(Session.history.getBackUrl())#"</cfoutput>;
}

The code above produce the following peace of HTML:

function back(){
      window.location = "

            http://dummy_server_address/index.cfm?TargetUrl=disp_main";
}

Looking at the Coldfusion specs here is the trim definition : A copy of the string parameter, after removing leading and trailing spaces and control characters.

So it should have done the job! I am therefore wondering how to do that properly, I don't want to use replace or some similar function.

EDIT : very surprisingly this is working... but I don't like this solution, so if you have any other idea, or at least explanations about this behaviour.

    <cfset backUrl = Session.history.getBackUrl()>
    function back(){            
            window.location = <cfoutput>"#backUrl#"</cfoutput>;
    }

Upvotes: 0

Views: 717

Answers (2)

Peter Boughton
Peter Boughton

Reputation: 112240

Make sure your History component has output disabled. i.e:

 <cfcomponent output=false >

Then make sure the getBackUrl function (and every other function) in the CFC also has output=false set.

Also, don't forget to use JsStringFormat on the variable, to ensure it is appropriately escaped:

<cfoutput>"#JsStringFormat( Session.history.getBackUrl() )#"</cfoutput>

Otherwise, there's a potential risk for JavaScript injection, or just JS errors, if the URL happens to contain ".

Upvotes: 10

Tyran67
Tyran67

Reputation: 47

I've tested your current code and it works fine for me, I suspect that your CFC might be returning more then you think, which I obviously can't duplicate. I would personally always ensure that the component returns 'clean' results rather then removing junk characters after the fact :)

I have had similar issues in the past and it has always turned out to do with cfoutput, never got to the bottom of it. As a starting point I would rewrite this way and see if it makes a difference...

<cfset variables.stWindowLocation = '"' & Trim(Session.history.getBackUrl()) & '"'> 
<cfoutput>    
  function back() {            
    window.location = #variables.stWindowLocation#;}
</cfoutput>

Upvotes: 1

Related Questions