我有这个HTML代码:
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display an alert box:</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
alert("I am an alert box!");
}
</script>
</body>
</html>
然后我将此代码保存为: C:/js.html
然后我在浏览器的地址栏中写道:
文件:/// C:/js.html#javascript:myFunction的();
但是没有执行Javascript功能。为什么呢?
我该如何做到这一点?
答案 0 :(得分:2)
Try this short snipped. Visit your page with #test as location part of your url index.html#test.
function myHash() {
alert('here iam!');
}
function hash() {
var hash = location.hash;
switch(hash) {
case '#test' : myHash();
break;
default : break;
}
}
hash();
答案 1 :(得分:1)
you need to put your script tag between the head tags like so
<!doctype html>
<html>
<head>
<script>
function myFunction() {
alert("I am an alert box!");
}
</script>
</head>
<body>
<p>Click the button to display an alert box:</p>
<button onclick="myFunction()">Try it</button>
</body>
</html>
答案 2 :(得分:0)
这就是你要的
<script type="text/javascript">
if(window.location.hash)
eval(window.location.hash.substr(1))
</script>
恭喜!您刚刚造成了XSS漏洞。当心site.html#deleteuseraccount()
更好的方法,但对您的问题的回答错误。
<script>
function() {
if (location.hash === "#magicword") {
YourMagicHere();
}};
</script>