Amr El Aswar
Amr El Aswar

Reputation: 3525

Run javascript code in android browser via an intent

Ok so I was using :

String url ="http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

To send the user to visit a url on his device's browser then I was wondering if it would be possible to send a javascript code via an intent to be run on the devices browser something like javascript:alert('hello') because typing that in the browser will run it so is it possible to do it via an intent ? If it is possible wouldnt that be some sort of security flaw in android ? Any documentation or example would be nice for further ressearch. Thanks

I would also like to mention that I have tried to pass javascript code via the setData() but it just froze the UI without giving any error.

Upvotes: 2

Views: 3258

Answers (2)

Alex
Alex

Reputation: 616

Here is full guide about using JavaScript in Android web-view apps: https://developer.android.com/guide/webapps/webview.html

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007339

because typing that in the browser will run it

That would depend on the browser. There are many Web browser implementations for Android. I would not assume that all Web browsers on Android support the javascript scheme in the address bar, though some might.

so is it possible to do it via an intent ?

That would depend on the browser. There are many Web browser implementations for Android. In general, I would not expect javascript to be a supported scheme, but there may be workarounds for that limitation.

If it is possible wouldnt that be some sort of security flaw in android ?

Possibly. It would depend on the browser and how it handled the Intent. There are many Web browser implementations for Android. It would not be a security flaw in Android, any more than bugs in Windows apps represent security flaws in Windows.

Upvotes: 1

Related Questions