0

I have some code that runs and does an XML request to a web service. This web service then returns a url. I need to take the user to that url.

At the moment the url is opening in an iframe, which was fine. However changes are being made to the site and it now needs to open in a new tab. I have seen a few javascript examples of how to open a url in a new tab, but all of them require an onclick event. Also, my javascript skills are very limited. Is there any code I could use to open the url , returned by the webservice. in a new tab without having to use a click event?

At the moment this is my code inside of the iframe. I know that the iframe would have to be removed and other changes will be made. I just want to give you an idea of how the webservice is called etc.

<cftry>
    <cfinclude template="webservices/travelit_request.cfm">
    <cfset mynewURL = "#XML_URL#">
    <cfset mynewURL &= "&adref=0">
    <cflocation url="#mynewURL#" addtoken="false">

    <cfcatch type="any"><h1>Your request was cancelled. Please retry.</h1></cfcatch>
</Cftry>
Community
  • 1
  • 1
  • Any js code that will execute with an onclick event will load on any event. It sounds like you want the onload event for the html body tag. Or, if you don't put the js in a function, it will simply run on the page request. – Dan Bracuk Aug 08 '14 at 09:35
  • Could you maybe give me a snippet or an example to show how i would do that? – user3917244 Aug 08 '14 at 09:46
  • Are you referring to clicking a link and having the URL open in a new tab? If so, you can use the `target` attribute of the anchor tag. Simply set the value to `_blank` – Scott Stroz Aug 08 '14 at 11:23
  • No the user basically does nothing, they login and when they login i check everything and then use a webservice to return a unique url for them to go to, i need to open that url in a new window after all the authentication etc? – user3917244 Aug 08 '14 at 11:52
  • New window or new tab? They are not the same. – Dan Bracuk Aug 08 '14 at 11:58
  • new tab sorry, same window just a new tab – user3917244 Aug 08 '14 at 12:10
  • 1
    What do you mean by "tab"? A new _browser_ tab or some sort of new _ui / Javascript_ tab? If the former than just add `target="_blank"` to your link and the browser will default to opening in a new _browser_ tab. If the latter than you need to tell us what Javascript library you are using to create your tabbed interface (jQuery UI, etc). – Miguel-F Aug 08 '14 at 12:37
  • This question is same as http://stackoverflow.com/questions/14347092/redirect-to-a-new-tab-using-cflocation-cf9 – Sathish Chelladurai Aug 08 '14 at 16:31

2 Answers2

0

<cflocation> performs a client-side redirect, but it's initiated on the server side (it sends a request with a redirect in the header), so it can't know anything about "tabs" which are a browser thing. CF doesn't know anything about what's going on in the browser.

To do the sort of thing you want to do on the client site, you need to do the browser stuff with Javascript.

-- Adam Cameron

For your reference. Redirect to a new tab using CFLocation - CF9

Thanks to Adam Cameron

Community
  • 1
  • 1
Sathish Chelladurai
  • 670
  • 1
  • 8
  • 23
0
<cfoutput><cftry>
    <cfinclude template="webservices/travelit_request.cfm">
    <cfset mynewURL = "#XML_URL#">
    <cfset mynewURL &= "&adref=0">
    <cfoutput>
    <script language="javascript">
      window.open('#mynewURL#','_blank');
    </script></cfoutput>

    <A href="#mynewURL#" target="_blank">Page didn't open? Click Here.</A>
<cfcatch type="any"><h1>Your request was cancelled. Please retry.</h1></cfcatch>
</Cftry></cfoutput>

This would work, it leaves the page you're on where it's at, and opens a new page, offering a link if it didn't open.

Of course, this should actually be in the proper place in your document's output.

Regular Jo
  • 5,190
  • 3
  • 25
  • 47