1

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?

4

3 回答 3

0

不导航文档就无法更改地址栏。这是事实。

当谈到使用伪协议处理程序在地址栏中执行 javascript 时,请参阅我在空 JavaScript 函数如何实际做某事javascript的回答?

于 2010-05-27T09:25:48.800 回答
0

像这样的东西怎么样:

javascript:function f(){return 42}; var r = f(); alert("The result is " + r); void(0);
于 2010-05-27T06:11:48.877 回答
0

所以如果我明白这就是你想要的:

  • 在您创建的页面上,您想在地址栏中输入 javascript
  • 在页面中 - 您想从地址栏中输入的函数中获取结果

我不确定我是否理解您想要这样做的原因,所以如果您这样描述,也许我的回答不会很好。

但是,如果您在页面上创建了一个名为 test() 的函数,并且在地址栏中输入 javascript 时位于该页面上,则可以将数据发送到该函数,从而捕获一个参数

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

如果您在页面上有此 javascript,它将被调用并出现带有文本“成功”的警报。

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

不过我必须说,除了在页面上测试 javascript 函数之外,我无法想象何时使用它。

于 2010-05-27T08:00:55.540 回答