use of iframe ajax in android browser - android

I have a html page which is used in a android application . From this page i need to post data to server and response is an attachment which needs to be downloaded. I used the hidden iframe hack for this purpose. But unfortunately its failing. Can anyone explain the root cause?
function download(){
var iframe = document.createElement("iframe");
//iframe.src = "http://localhost:9080/HttpOptions/MultiPartServlet";
iframe.style.display = "none";
iframe.id = "myframe";
document.body.appendChild(iframe);
var doc = document.getElementById("myframe").contentWindow.document;
var form = doc.createElement("form");
form.setAttribute("name", "theForm"); // give form a name
form.setAttribute("id", "theForm"); // give form a name
form.setAttribute("action", "http://localhost:9080/HttpOptions/MultiPartServlet"); // give form an action
form.setAttribute("method", "post"); // give form a method
var par1 = doc.createElement("input");
par1.setAttribute("name", "theSubmit"); // give input a name
par1.setAttribute("type", "submit"); // make it a submit button
par1.setAttribute("value", "Submit"); // give input a value
var par2 = doc.createElement("input");
par2.setAttribute("name", "name"); // give input a name
par2.setAttribute("type", "text"); // make it a submit button
par2.setAttribute("value", "deepak"); // give input a value
form.appendChild(par1);
form.appendChild(par2);
doc.body.appendChild(form);
var myframe = document.getElementById('myframe');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
form.submit();
}

Set the document.domain to same value in both parent page and framed page.
Example:
<script type="text/javascript">
document.domain = 'example.com';
</script>
Put this in both parent page and framed page, and the problem will be gone.

Related

Print a web page from a webview

I made an app that is just a webview showing a website, at a certain point in my website I have a button that prints a div via javascript, as it is it doesn't work, how can I make it possible?
my js code:
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
}

Is there any way to transfer data say variable defined in inappbrowser and pass its value in executeScript method?

I want to use variable defined in inAppBrowser of cordova and use it in in executeScript method?
ref = cordova.InAppBrowser.open(env,'_blank','clearcache=no,location=no,
clearsessioncache=no, footer=no,zoom=no' );
var variable_needed = 1;
ref.addEventListener("loadstop", function(e) { ref.executeScript({
code: "console.log(variable_needed);"})
});
When I am trying to access varible_in executescript, it gives NULL.
I know they are different javascript bundle. Is there any way to achieve this?
The value passed via the code key of executeScript() is passed as a string to the Java layer which injects it into the Webview of the Inappbrowser and eval()'s it.
As such, there is no direct "connection" between the Cordova app Webview and the InappBrowser Webview that would allow Javascript entities to be directly shared.
Since the data is passed as a string, only stringifiable Javascript data can be passed (i.e. not functions).
In your case above, the variable value should simply be inlined in the code string:
ref = cordova.InAppBrowser.open(env,'_blank','clearcache=no,location=no, clearsessioncache=no, footer=no,zoom=no' );
var variable_needed = 1;
ref.addEventListener("loadstop", function(e) {
ref.executeScript({
code: "console.log("+variable_needed+");"
});
});
However if you want to send a more complex object structure you would need to stringify it, for example:
ref = cordova.InAppBrowser.open(env,'_blank','clearcache=no,location=no, clearsessioncache=no, footer=no,zoom=no' );
var myObj = {
foo: "bar"
};
var s_myObj = JSON.stringify(myObj);
ref.addEventListener("loadstop", function(e) {
ref.executeScript({
code: "var myObj = JSON.parse('"+s_myObj+"'); console.dir(myObj);"
});
});

How to acces shared object and display on facebook/Twitter URL Link AS3

Hey everyone so I was wondering if there is a easy method of doing this or if it can even be done without an ANE Native Extension. I picked up some code from around here and in my Flash CS6 Android AIR Application using AS3 I created two buttons on the stage one for Facebook and the other for Twitter. When the user clicks on the buttons they are redirected to the URL's and are able to post the link that I give in the vars like so:
//Share button event listener
menuEnd.share_Facebook.addEventListener(MouseEvent.CLICK, shareFacebook);
menuEnd.share_Twitter.addEventListener(MouseEvent.CLICK, shareTwitter);
private function shareTwitter(e:MouseEvent):void
{
var varsShare:URLVariables = new URLVariables();
varsShare.u = 'https://play.google.com/store/apps/details?id=air.bunnyRunner';
varsShare.t = 'Jumpy Bunny';
var urlTwitterShare:URLRequest = new URLRequest('http://twitter.com/home?status= Jumpy Bunny by Fitchett Productions: ');
urlTwitterShare.data = varsShare;
urlTwitterShare.method = URLRequestMethod.GET;
navigateToURL(urlTwitterShare, '_blank');
}
private function shareFacebook(evt:MouseEvent):void
{
var varsShare:URLVariables = new URLVariables();
varsShare.u = 'https://play.google.com/store/apps/details?id=air.bunnyRunner';
varsShare.t = 'Jumpy Bunny';
var urlFacebookShare:URLRequest = new URLRequest('http://m.facebook.com/sharer.php');
urlFacebookShare.data = varsShare;
urlFacebookShare.method = URLRequestMethod.GET;
navigateToURL(urlFacebookShare, '_blank');
}
So now I was wondering since I have sharedObject data in my game is there anyway to get that shared.data and display it on facebook when the user clicks the button it shares the data to facebook and they can post their highscore?
Thanks guys any help is appreciated.

Calling an External Javascript Function

I have an app i'm programming in Javascript/JQuery. I'm using PhoneGap and want to use localStorage to store the users credentials the first time they open the app and then autofill next time the user opens the app. I want to call the checkPreAuth() right when the page loads. But It's not calling my function. Can anybody help me?
Calling the function from login.html:
<script>
$(document).ready(function() {
checkPreAuth();
});
</script>
And the function in my digest_auth.js:
function checkPreAuth() {
var form = $("#loginForm");
var values = new Array();
var username = $('#username').val();
var password = $('#password').val();
if(window.localStorage["username"] == undefined && window.localStorage["password"] == undefined) {
localStorage.setItem("username", username);
localStorage.setItem("password", password);
alert('Saved username and password');
} else {
alert(username + ', ' + password);
}
}
Maybe this is the answer to your problem. I faced this problem and instead of using document ready of jquery, i used this:
JQuery document.ready vs Phonegap deviceready

appcelerator titanium - Ti.App.Properties not working on android

I am creating an (iphone/android) mobile app using appcelerator titanium. I have a problem using Ti.App.Properties,
I want to save the user's login data (username and password), I used Ti.App.Properties's getList and setList methods to get and set username and password at app startup. It is working fine on iPhone, but on android the data (username and password) are not retrieved at app startup.
here is the code that is executed at app startup :
var userDataArray=[{title:'name',value:''},
{title:'password',value:''}];
if(Ti.App.Properties.hasProperty("userDataArray"))
{
userDataArray = Ti.App.Properties.getList("userDataArray");
}
else
{
Ti.App.Properties.setList("userDataArray",userDataArray);
}
if((Ti.App.Properties.getList("userDataArray")[0].value.length==0)||(Ti.App.Properties.getList("userDataArray")[1].value.length==0))//check if name, password have no values.. on android, this is always the case, which is not correct
{
//go to login page
}
else if((Ti.App.Properties.getList("userDataArray")[0].value.length>0)&&(Ti.App.Properties.getList("userDataArray")[1].value.length>0))//if both username and password exist
{
//start
}
Thank you
i think your overall approach is flawed, you dont need an array just an map
// save the values as a string..
Ti.App.Properties.setString({"username":"myname", "password":"mypassword"}, "CREDENTIALS");
// retrieve the values as a string, but parse it back into an object
var credObject = JSON.parse(Ti.App.Properties.getString("CREDENTIALS"));
// dump the output
Ti.API.debug("Username "+ credObject.username);
Ti.API.debug("Password "+ credObject.password);
two remarks :
arguments for .setString() is opposite, ie Name then Value
Value must be a string, so you have to stringify() it or enter it as a string
I know this is old, but it's still relevant today as there's not a huge amount of help with Titanium. I handle this in two parts.
Part 1) After the user's credentials have been authenticated...
var username = "some username";
var password = "some password";
// Build the object and then convert it to a json string.
oCredentials = new Object();
oCredentials.username = username;
oCredentials.password = password;
var stringCredentials = JSON.stringify(oCredentials);
// Save the credentials
Ti.App.Properties.setString("Credentials", stringCredentials);
Part 2) Before you prompt the user with the login window/popup/whatever...
// Look for credentials
(function() {
var storedCredentials = Ti.App.Properties.getString("Credentials");
if (storedCredentials){
var oJson = JSON.parse(storedCredentials);
// Call your authentication function
// For example, autoAuthenticate(oJson.username, oJson.password);
} else {
// kick the user out to your login window
// For example, $.loginWindow.open();
}
})();

Categories

Resources