Call Javascript function from URL/address bar

JavascriptUrl

Javascript Problem Overview


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: http://www.example.com/mypage.aspx?javascript:printHelloWorld()

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.

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

Write in address bar

javascript:alert("hi");

Make sure you write in the beginning: javascript:

Solution 3 - Javascript

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

For more information visit: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs

Solution 4 - Javascript

/test.html#alert('heello')

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

Solution 5 - Javascript

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

Solution 6 - Javascript

About the window.location.hash property:

Return the anchor part of a URL.


Example 1:

//Assume that the current URL is 

var URL = "http://www.example.com/test.htm#part2";

var x = window.location.hash;

//The result of x will be:

x = "#part2"

Exmaple 2:

$(function(){	
	setTimeout(function(){
		var id = document.location.hash;
		$(id).click().blur();
	}, 200);
})

Example 3:

var hash = "#search" || window.location.hash;
window.location.hash = hash; 
  
switch(hash){   
case "#search":  
    selectPanel("pnlSearch");
    break;    
case "#advsearch":    
      
case "#admin":  
     
}

Solution 7 - Javascript

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 :

$(document).ready(function(){
  var hash = window.location.hash;
  if(hash.length > 0){
	// your action with the hash
  }
});

Solution 8 - Javascript

you can use like this situation: for example, you have a page: http://www.example.com/page.php 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: http://www.example.com/page.php?doaction=blabla

then the alert will be automatically called.

Solution 9 - Javascript

Just use:

(function() {
  var a = document.createElement("script");
  a.type = "text/javascript";
  a.src = "http://www.example.com/helloworld.js?" + Math.random();
  document.getElementsByTagName("head")[0].appendChild(a)
})();

This basically creates a new JavaScript line in the head of the HTML to load the JavaScript URL you wish on the page itself. This seems more like what you were asking for. You can also change the a.src to the actual code, but for longer functions and stuff it becomes a problem. The source link can also link to a JavaScript file on your computer if targeted that way.

Solution 10 - Javascript

you can execute javascript from url via events Ex: www.something.com/home/save?id=12<body onload="alert(1)"></body>

does work if params in url are there.

Solution 11 - Javascript

You can do one thing that is you can first open the link www.example.com. Then you can search: javascript:window.alert("Hello World!")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDazManCatView Question on Stackoverflow
Solution 1 - JavascriptNick CraverView Answer on Stackoverflow
Solution 2 - JavascripthfarazmView Answer on Stackoverflow
Solution 3 - Javascriptyassine45View Answer on Stackoverflow
Solution 4 - JavascriptBudiaView Answer on Stackoverflow
Solution 5 - JavascriptheximalView Answer on Stackoverflow
Solution 6 - JavascriptEddyView Answer on Stackoverflow
Solution 7 - JavascriptEllemKahView Answer on Stackoverflow
Solution 8 - JavascriptT.ToduaView Answer on Stackoverflow
Solution 9 - JavascriptBrandonView Answer on Stackoverflow
Solution 10 - JavascriptMurari MahithView Answer on Stackoverflow
Solution 11 - JavascriptSagnik ChakrabortyView Answer on Stackoverflow