2013-04-03 38 views
-1

我的问题是:运行JavaScript通过URL

我有一个eibport: www.bab-tec.de/

你可以看到在现场部分现场演示。

现在的问题。

我想启动或运行此页面上的JavaScript和输入URL,如: http:// IP-EIBPORT /#javascript:click(Button)

或以另一种方式。

有人可以帮助我吗?

+2

如果你这样做,任何人都可以调整你的网址发送恶意脚本... – Christophe 2013-04-03 20:24:26

回答

0

您不能在URL中包含JavaScript调用代码。您可以做的最好的是在目标页面上有一个脚本,用于检查位置,并做出相应响应。

举一个例子,假设你想通过url公开sum方法。如果有人使用以下查询字符串加载页面:?method=sum&params=5,5,则可以使用逻辑来查找这些部分并对其进行处理。

var methods = { 
    sum: function() { 
     var result = 0; i = -1; 
     while (++i < arguments.length) 
      result += parseInt(arguments[i], 10); 
     return result; 
    } 
}; 

var method = location.search.match(/method=(.*)&/)[1], 
    params = location.search.match(/params=(.*)$/)[1].split(","); 

var result = methods[ method ].apply(this, params); 

alert(result); 

看到它在行动:http://jsfiddle.net/jonathansampson/TF5ta/show/?method=sum&params=5,5

1

您可能需要设置你的脚本来寻找一个sepcific哈希和运行基于该功能。

switch(location.hash){ 
    case "#button": 
     //click button 
    break; 
    case "#other": 
     //do something else 
    break; 
} 

然后使用网址像/#button/#other


的方式做的正是你想要的是下面的,但它不推荐!

eval(location.hash.substr(1)); 

使用像/#alert('test');一个网址,将工作,但它意味着任何人都可以链接到你的网站和执行JavaScript这是做一个非常不安全的事情。