0

I have a link on my page with href="javascript:sayhello()", is it possible to add this function to full url? Somethink like: http://example.com/page.html+javascript:sayhello()

MrBinWin
  • 1,269
  • 1
  • 16
  • 30
  • And what should that do? Load the link and then execute the JavaScript on the subsequent page? – David Thomas Jan 31 '15 at 21:45
  • Yes, load page and call the function – MrBinWin Jan 31 '15 at 21:48
  • You can use [**this example code**](http://jsfiddle.net/0aqkk3j4/) to achieve that, **note it will not work in jsfiddle, you have to import it to your website**. After pasting that code in a ` – undefined Jan 31 '15 at 22:00

1 Answers1

1

No, that's not possible.

The part of the URL before the : says how the rest of the URL should be interpreted. When it starts with http:, it means that the link should make a GET request to an HTTP server, and request the page whose path is named in the rest of the URL. No Javascript is interpreted as part of that.

When it starts with javascript:, it means that instead of downloading a page from a webserver, it should execute the rest of the URL as Javascript, similar to the element having an onclick.

You could, however, write Javascript that redirects to the page:

<a href="javascript:sayhello(); location.href='http://example.com/page.html';">
Barmar
  • 741,623
  • 53
  • 500
  • 612