当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Open InMobi iframe link in device browser (phonegap build)

问题描述:

I have created a mobile app using jQuery Mobile and built it for iOS and Android via PhoneGap Build v2.5. I then successfully added inmobi advertisements using their javascript api. The ad displays just fine but the problem comes when a user clicks the ad as the ad takes over the app. This isn't such an issue in Android as users have a back button but on iOS devices the user is stuck and unable to get back to my app.

I have successfully set up all external links contained in my app to open in the device's browser by passing all url's to this function:

function openNewBrowser(url) {

window.open(encodeURI(url), '_system');

return false;

}

But the inmobi ad is inside of an iframe in my app and I have no control over how they pass their url's. I have also tried to bind jQuery Mobile to catch all pagebeforechange events and process them accordingly using:

$(document).bind("pagebeforechange", function(e, data) {

if(typeof data.toPage === 'object' ||

data.toPage.indexOf("index.html#") >= 0) {

//internal URL so do nothing

} else {

//external URL so send to openNewBrowser

console.log('page is external');

openNewBrowser(data.toPage);

e.stopPropagation();

return false;

}

});

but the "else" is never tripped. The inmobi ad just takes over the app and user is forced to kill and restart the app back to a usable state. Is there a different event I should be listening for?

Here is how I am calling the inmobi ad:

var inmobi_conf = {

siteid : "*******mySiteId*******",

slot : "15",

manual: true,

test: true,

targetWindow: "_blank"

};

I would (and have tried to) set the "targetWindow" to _system but the only valid parameters are "_blank" and "_top".

Does anyone know of a way to get iFrame links (that you cant set to _system) to open in the native browser instead of taking over the app or gotten inmobi ad's to not take over the app?

网友答案:

I ended up working around this issue by setting in the config.xml. This is a terrible work-around but its the best I have. Hopefully someone else can come up with something better.

Using a dummy iframe as the target would also work:

/*
document.body.insertAdjacentHTML
  ("beforeend", 
    "<iframe id='myframe' width='0' height='0'></iframe>"
  )
*/

document.body.appendChild
  (
  document.createElement("iframe")
  ).setAttribute("id", "myframe")

var inmobi_conf = {
    siteid : "*******mySiteId*******",
    slot : "15",
    manual: true,
    test: true,
    targetWindow: "myframe"
};

References

  • HTML Training, Anchor Elements: Link Options
分享给朋友:
您可能感兴趣的文章:
随机阅读: