I have this HTML code:
<!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>
Then I save this code as: C:/js.html
Then I write in the address-bar of my browser:
file:///C:/js.html#javascript:myFunction();
But the Javascript function is not executed. Why?
How can I make this work?
asked Jul 28, 2015 at 22:02
user1580348
6,0996 gold badges54 silver badges131 bronze badges
-
You might consider using location.hash to dertmine a function to invoke.user4164128– user41641282015年07月28日 22:05:08 +00:00Commented Jul 28, 2015 at 22:05
-
This code is not supposed to execute the function until you click on the buttonSebas– Sebas2015年07月28日 22:05:09 +00:00Commented Jul 28, 2015 at 22:05
-
Also, add a header section, and put the script inside.Sebas– Sebas2015年07月28日 22:05:33 +00:00Commented Jul 28, 2015 at 22:05
-
@fubbe Can you show me an example?user1580348– user15803482015年07月28日 22:07:09 +00:00Commented Jul 28, 2015 at 22:07
-
In comments below, you indicate you can't edit the webpage in question. That's important to note, and requires a lot more detail about what you want to do.Teepeemm– Teepeemm2015年07月29日 15:41:52 +00:00Commented Jul 29, 2015 at 15:41
3 Answers 3
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();
Sign up to request clarification or add additional context in comments.
8 Comments
user1580348
Thanks, this works. But I need to call ANY javascript from ANY website in this way, not just from mine. Is this possible?
user1580348
So is there absolutely no way to execute from the URL a javascript which is located or referenced inside the web-page? I mean, I don't even need to display the web-page, I only need to just execute the javascript.
Vidul
This works only on page load (i.e. does not observe URL changes). Otherwise replace
hash() with window.onhashchange = hash(); |
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>
answered Jul 28, 2015 at 22:08
hello world
1,7756 gold badges21 silver badges37 bronze badges
Comments
This is what you asked for
<script type="text/javascript">
if(window.location.hash)
eval(window.location.hash.substr(1))
</script>
Congratulation! You just caused an XSS vulnerability. Beware of site.html#deleteuseraccount()
Better way to do it but wrong answer to yourr question.
<script>
function() {
if (location.hash === "#magicword") {
YourMagicHere();
}};
</script>
answered Feb 6, 2020 at 17:37
user23139
3681 gold badge3 silver badges16 bronze badges
Comments
default