Call Javascript function from URL/address bar

Is it possible to call a javascript function from the URL? I am basically trying to leverage JS methods in a page I don't have access to the source.

Something like:

I know if you put javascript:alert("Hello World"); into the address bar it will work.

I suspect the answer to this is no but, just wondered if there was a way to do it.



There isn't from a hyperlink, no. Not unless the page has script inside specifically for this and it's checking for some parameter....but for your question, no, there's no built-in support in browsers for this.

There are however bookmarklets you can bookmark to quickly run JavaScript functions from your address bar; not sure if that meets your needs, but it's as close as it gets.


Write in address bar


Make sure you write in the beginning: javascript:


You can use Data URIs. For example: data:text/html,<script>alert('hi');</script>

For more information visit:



<button onClick="eval(document.location.hash.substring(1))">do it</button>

you may also place the followinng

<a href='javascript:alert("hello world!");'>Click me</a>

to your html-code, and when you click on 'Click me' hyperlink, javascript will appear in url-bar and Alert dialog will show


About the window.location.hash property:

Return the anchor part of a URL.

Example 1:

//Assume that the current URL is 

var URL = "";

var x = window.location.hash;

//The result of x will be:

x = "#part2"

Exmaple 2:

        var id = document.location.hash;
    }, 200);

Example 3:

var hash = "#search" || window.location.hash;
window.location.hash = hash; 

case "#search":  
case "#advsearch":    

case "#admin":  


Using Eddy's answer worked very well as I had kind of the same problem. Just call your url with the parameters : "www.mypage.html#myAnchor"

Then, in mypage.html :

  var hash = window.location.hash;
  if(hash.length > 0){
    // your action with the hash

you can use like this situation: for example, you have a page: then in that page.php, insert this code:

if (!empty($_GET['doaction']) && $_GET['doaction'] == blabla ){
echo '<script>alert("hello");</script>';

then, whenever you visit this url:

then the alert will be automatically called.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.