1

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?

5
  • You might consider using location.hash to dertmine a function to invoke. Commented Jul 28, 2015 at 22:05
  • This code is not supposed to execute the function until you click on the button Commented Jul 28, 2015 at 22:05
  • Also, add a header section, and put the script inside. Commented Jul 28, 2015 at 22:05
  • @fubbe Can you show me an example? Commented 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. Commented Jul 29, 2015 at 15:41

3 Answers 3

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();
Sign up to request clarification or add additional context in comments.

8 Comments

add it to your script tag and take a look at @hello world's structure.
Thanks, this works. But I need to call ANY javascript from ANY website in this way, not just from mine. Is this possible?
In short, nope. If you visit a page containing javascript, it will execute on pre defined purpose.
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.
This works only on page load (i.e. does not observe URL changes). Otherwise replace hash() with window.onhashchange = hash();
|
0

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>

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.