You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cordova.apache.org by "Shazron Abdullah (Created) (JIRA)" <ji...@apache.org> on 2011/11/29 00:05:40 UTC

[jira] [Created] (CB-62) Unable to use JavaScript to redirect to a different page.

Unable to use JavaScript to redirect to a different page.
---------------------------------------------------------

                 Key: CB-62
                 URL: https://issues.apache.org/jira/browse/CB-62
             Project: Apache Callback
          Issue Type: Bug
          Components: iOS
            Reporter: Shazron Abdullah


**Summary:** You can no longer redirect to a different page by updating `document.location.href`.

reported at: https://github.com/phonegap/phonegap-iphone/issues/270
by: https://github.com/buddydvd

**To reproduce:** Add `www.google.com` to `PhoneGap.plist`'s `ExternalHosts` array. Add an anchor link to `index.html` with its `onclick` handler set to use JavaScript to redirect the webView to`http://www.google.com`.

    <a href="#" onclick="document.location.href='http://www.google.com/'; return false;">click me</a>

**Expected:** Clicking the anchor link will load `http://www.google.com` in the embedded webView.
**Actual:** Clicking the anchor link loads `http://www.google.com` in Mobile Safari.

For more info, see: https://github.com/phonegap/phonegap-iphone/commit/9eaeceb2048427f2349cbd862af2507bddf08f77

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Resolved] (CB-62) Unable to use JavaScript to redirect to a different page.

Posted by "Shazron Abdullah (Resolved) (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/CB-62?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Shazron Abdullah resolved CB-62.
--------------------------------

    Resolution: Won't Fix

Workaround is to use the JS function here: 
https://gist.github.com/1725083

{code:javascript}
function openInWebView(url)
{
        var anchor = document.createElement('a');
        anchor.setAttribute('href', url);
        anchor.setAttribute('target', '_self');
        
        var dispatch = document.createEvent('HTMLEvents')
        dispatch.initEvent('click', true, true);
        
        anchor.dispatchEvent(dispatch);
}
{code}
                
> Unable to use JavaScript to redirect to a different page.
> ---------------------------------------------------------
>
>                 Key: CB-62
>                 URL: https://issues.apache.org/jira/browse/CB-62
>             Project: Apache Callback
>          Issue Type: Bug
>          Components: iOS
>            Reporter: Shazron Abdullah
>
> **Summary:** You can no longer redirect to a different page by updating `document.location.href`.
> reported at: https://github.com/phonegap/phonegap-iphone/issues/270
> by: https://github.com/buddydvd
> **To reproduce:** Add `www.google.com` to `PhoneGap.plist`'s `ExternalHosts` array. Add an anchor link to `index.html` with its `onclick` handler set to use JavaScript to redirect the webView to`http://www.google.com`.
>     <a href="#" onclick="document.location.href='http://www.google.com/'; return false;">click me</a>
> **Expected:** Clicking the anchor link will load `http://www.google.com` in the embedded webView.
> **Actual:** Clicking the anchor link loads `http://www.google.com` in Mobile Safari.
> For more info, see: https://github.com/phonegap/phonegap-iphone/commit/9eaeceb2048427f2349cbd862af2507bddf08f77

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Commented] (CB-62) Unable to use JavaScript to redirect to a different page.

Posted by "Shazron Abdullah (Commented) (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CB-62?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13158908#comment-13158908 ] 

Shazron Abdullah commented on CB-62:
------------------------------------

by: http://github.com/shazron commented September 13, 2011
Unfortunately, this method of changing the url through onclick and document.location will always result as the link being "UIWebViewNavigationTypeOther", even if it has a a target="_self". We check for UIWebViewNavigationTypeOther for anchor tags that have target="_blank" so we re-direct the url to Mobile Safari.

There does not appear to be an alternative, save doing this which is equivalent to what you expect:
<a href="http://www.google.com" target="_self">click me</a>


by: http://github.com/buddydvd commented September 13, 2011
I believe I have a solution.

When you click on an anchor link with target="_blank", the webView actually makes two calls to webView:shouldStartLoadWithRequest:navigationType:.

The first call is made with navigationType set to UIWebViewNavigationTypeLinkClicked and second one with UIWebViewNavigationTypeOther. This behavior seems to be unique to anchor links decorated with target="_blank".

Here's a relevant blog post: http://longweekendmobile.com/2010/09/14/making-target-_blank-links-work-in-a-uiwebview/

The solution I have in mind is a slightly modified version of the one presented in that post. I will update this thread when I have more information.


by: http://github.com/shazron commented September 13, 2011
Randy, window.location shows the same behaviour as the original problem (document.location).


by: http://github.com/shazron commented September 13, 2011
buddydvd - confirmed, it generate the two calls as you described (on 4.3 Simulator) for anchor tags with target="_blank". The problem to solve is (reliably) detecting that these two calls in succession constitute a target="_blank".


by: http://github.com/buddydvd commented September 13, 2011
@shazron: Thanks for the confirmation. I saw you've setup application tests for the project. Do you think it is ready to include new test cases that simulate clicking on anchor links?


by: http://github.com/shazron commented September 13, 2011
@buddydvd - I've had trouble integrating a UIWebView in there for logic tests, but it's possible for application tests (haven't had time to research how yet). I assume the test you are envisioning will require a UIWebView...


by: http://github.com/buddydvd commented September 13, 2011
@shazron: Yes, those tests will definitely require automating an UIWebView.

Some of the test cases I can think of are:

Redirect by sending click event to an anchor without the target attribute.
Redirect by sending click event to an anchor with target="_self".
Redirect by sending click event to an anchor with target="_top".
Redirect by sending click event to an anchor with target="_blank".
Redirect by sending click event to an anchor with target="randomValue".
Redirect by setting document.location.href
And possibly these too:

Redirect by calling UIWebView's loadRequest: method (through a plugin).
Load remote URL by overriding PhoneGapDelegate's startPage class method.

by: http://github.com/apemberton commented October 19, 2011
Any update on this?


by: http://github.com/buddydvd commented October 19, 2011
@apemberton: Sorry, I haven't got to work on it because I got side tracked and wasn't sure how to write test cases for it.
                
> Unable to use JavaScript to redirect to a different page.
> ---------------------------------------------------------
>
>                 Key: CB-62
>                 URL: https://issues.apache.org/jira/browse/CB-62
>             Project: Apache Callback
>          Issue Type: Bug
>          Components: iOS
>            Reporter: Shazron Abdullah
>
> **Summary:** You can no longer redirect to a different page by updating `document.location.href`.
> reported at: https://github.com/phonegap/phonegap-iphone/issues/270
> by: https://github.com/buddydvd
> **To reproduce:** Add `www.google.com` to `PhoneGap.plist`'s `ExternalHosts` array. Add an anchor link to `index.html` with its `onclick` handler set to use JavaScript to redirect the webView to`http://www.google.com`.
>     <a href="#" onclick="document.location.href='http://www.google.com/'; return false;">click me</a>
> **Expected:** Clicking the anchor link will load `http://www.google.com` in the embedded webView.
> **Actual:** Clicking the anchor link loads `http://www.google.com` in Mobile Safari.
> For more info, see: https://github.com/phonegap/phonegap-iphone/commit/9eaeceb2048427f2349cbd862af2507bddf08f77

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira