Skip to content

Instantly share code, notes, and snippets.

@shazron
Created February 2, 2012 18:49
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shazron/1725083 to your computer and use it in GitHub Desktop.
Save shazron/1725083 to your computer and use it in GitHub Desktop.
PhoneGap/Cordova: Open a url in the same UIWebView programmatically
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);
}
@shazron
Copy link
Author

shazron commented Feb 10, 2012

usage in PhoneGap:
If you want "http://google.com" to open inside the current UIWebView, call it like this (after including the code above in your HTML page or JavaScript file):

openInWebView('http://google.com')

Make sure you added google.com in your PhoneGap.plist/ExternalHosts whitelist

@avatasia
Copy link

Hi, if I click a link in google.com ,then it will open in a new safari window, how to fix this?

@shazron
Copy link
Author

shazron commented Mar 30, 2012

sorry, remove the line anchor.setAttribute('target', '_self');

@joe1chen
Copy link

Is this function necessary anymore? With the latest PhoneGap 2.2.0, I just wrote:

window.location.href = url;

@abdoroot
Copy link

its not work too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment