views:

114

answers:

3

Hi,

I have a small problem. When im executing a javascript in an address bar and the function that i'm calling there returns a value. The page moves to a new page. To prevent this, i use a void(0) at the end. But once this is done, how can i capture the returned value of the function.

For eg:

javascript:function f(){return 'success'} f();void(0);

How do i capture the return value, when i type this in an address bar?

A: 

What about something like this:

javascript:function f(){return 42}; var r = f(); alert("The result is " + r); void(0);
David Wolever
that would be very helpful if we wanted to just see the result. But, in my situation, I want to capture it from outside. So, for example, if there is a way to put it in the address bar or so, that would solve my problem.
kambamsu
So, something like `<a click="document.href='javascript:alert(\"' + foo + '\")">click me</a>`?
David Wolever
A: 

Hi,

so if I understand thisis what you want:

  • On a page you have created you want to enter a javascript in the address bar
  • In the page - you want to catch the result from the function you enter in the address bar

I'm not sure I understand the reason why you'd want to do this, so if you describe that perhaps my answer will not be very good.

But if you've created for example a function called test() on a page, and you are on that page when entering the javascript in the address bar, you are able to send the data into that function, and thus capturing the value in a parameter

javascript:function f(){return 'success'} test(f());void(0);

If you have this javascript on the page, it will be called and an alert with the text "success" will appear.

<script type="text/javascript">
function test(x)
{
 alert(x);
}
</script>

I must say though, I can't imagine when to use this other than perhaps testing javascript functions on a page.

becquerel
A: 

It is not possible to alter the address bar without navigating the document. That is a fact.

When it comes to executing javascript in the address bar using the pseudo protocol handler javascript see my answer at http://stackoverflow.com/questions/2865090/2865210#2865210

Sean Kinsey