We help IT Professionals succeed at work.

Executing a javascript function from url

btocakci
btocakci asked
on
hi, for some special reasons of mobile development (restrictions of cross-device platform called Marmalade API) i need a case like this:

From browser i will load a page. (That is normal)

On browser i need to suppy a URL for that:

Page will not be refreshed again but a javascript function inside the page will be executed.

Currently i can execute a function by some tricks. This is my url:

www.somedomain.com?action=alert('asd')

This really alerts what i want, but by refreshing on each request. I just need a feature such Firebug does. I tried bookmarklets and iframes with no success. Maybe you can help me on execute the js function without really requesting the page.This is really urgent. Thanks for your help.

Comment
Watch Question

Hey!
There's a way to do this without refreshing. By using javascript: you can run any kind of javascript live in most browsers without having to reload the page.

When you have loaded the page, change your url from:

www.somedomain.com

To alert a string:
javascript:alert('This will be alerted');

To check the value of a variable:
javascript:alert('What value is foo? It is: ' + foo);

Run a method:
javascript:myFunction();

Hope that helps!

Cheers,
Chris
In case it wasn't clear, you just switch out your url with the javascript:... and submit it to fire the javascript.

Author

Commented:
So what will be the exact value on browser's URL area? Simply this?

javascript:alert('This will be alerted');

It did nothing. Sorry for my lack of knowledge on this.

Author

Commented:
Because this url will be supplied by mobile application. Not by user clicking a link or sth.
Okay, what browser are you using to test this in at the moment?

Author

Commented:
firefox 7.0.1
Then there really shouldn't be any problems. Once your page is loaded, writing javascript:alert('Hello world'); and pressing enter should give you an alert box in that browser.

However if this will be applicable to you when you're using it in a mobile application, I'm not so sure of. Will the mobile application change the in-app-browsers url? Or if it's just for debugging wouldn't you be able to use the previous mentioned Firebug to do this?

Cheers,
Chris

Author

Commented:
Yes, in-app browser's url will be changed by application.

Author

Commented:
ok, it also worked in my environment. Thanks.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.