سؤال

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?

هل كانت مفيدة؟

المحلول

What about something like this:

javascript:function f(){return 42}; var r = f(); alert("The result is " + r); void(0);

نصائح أخرى

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.

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 How can empty JavaScript function actually do something?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top