0

There are several links (and pieces of Javascript) throughout our site that contain /2012/responses/{pathParams...} and I now need to conditionally add to the path, if it's a webview.

if (isWebview) {
  // use '/2012/responses/webview/{pathParams...}'
} else {
  // use '/2012/responses/{pathParams...}'
}

I can handle the links by inspecting document.links, but I'm wondering if there's a way to handle pieces of Javascript that use window.location = '/2012/responses/...'. One method is to create a function that does the window.location change and replace the window.location statements with the function. But, is there a way to handle it as an event, so when the page is changing I could conditionally insert the /webview in the URL? Browser restrictions seem to limit the beforeunload event to only prompting the user.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ravun
  • 1,523
  • 9
  • 27
  • 45
  • 2
    Your question implies you have lots of unrelated bits of JavaScript working separately and compensating/correcting for other bits. Why haven't you collated all of that into a single coherent function to handle URL (or `href`) updating? – David Thomas Sep 28 '12 at 20:36
  • @DavidThomas really has the solution. – Jeremy J Starcher Sep 28 '12 at 20:50

1 Answers1

0

I think you can't. I have 2 options for you:

Option 1:

Do a replace all to your code using ternary inline if --> i recommend that

sample:

search for:

    '/2012/responses/{pathParams...}'

replace with:

    '/2012/responses/'+((isWebView?)?'webview/':'')+'{pathParams...}'

Option 2:

Put the /webView conditionally on server side inside the {pathParams...} variable

falsarella
  • 12,217
  • 9
  • 69
  • 115